Templating: Add default ref id for data source variable queries (#35923)

* add default ref id for ds variable queries

* cleanup
This commit is contained in:
Erik Sundell 2021-06-18 14:02:51 +02:00 committed by GitHub
parent c51af73bf6
commit 7ff26bdfae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 1 deletions

View File

@ -1,4 +1,4 @@
import { QueryRunners } from './queryRunners';
import { QueryRunners, variableDummyRefId } from './queryRunners';
import { getDefaultTimeRange, VariableSupportType } from '@grafana/data';
import { VariableRefresh } from '../types';
import { of } from 'rxjs';
@ -293,6 +293,14 @@ describe('QueryRunners', () => {
const target = runner.getTarget({ datasource, variable });
expect(target).toEqual({ refId: 'A', query: 'A query' });
});
describe('and ref id is missing', () => {
it('then it should return correct target with dummy ref id', () => {
const { runner, datasource, variable } = getDatasourceTestContext();
delete variable.query.refId;
const target = runner.getTarget({ datasource, variable });
expect(target).toEqual({ refId: variableDummyRefId, query: 'A query' });
});
});
});
describe('and calling runRequest', () => {

View File

@ -149,6 +149,8 @@ class CustomQueryRunner implements QueryRunner {
}
}
export const variableDummyRefId = 'variable-query';
class DatasourceQueryRunner implements QueryRunner {
type = VariableSupportType.Datasource;
@ -158,6 +160,9 @@ class DatasourceQueryRunner implements QueryRunner {
getTarget({ datasource, variable }: GetTargetArgs) {
if (hasDatasourceVariableSupport(datasource)) {
if (!variable.query.refId) {
variable.query.refId = variableDummyRefId;
}
return variable.query;
}