From 7ff26bdfae25c19acb6cd6210509d0de22f43d1c Mon Sep 17 00:00:00 2001 From: Erik Sundell Date: Fri, 18 Jun 2021 14:02:51 +0200 Subject: [PATCH] Templating: Add default ref id for data source variable queries (#35923) * add default ref id for ds variable queries * cleanup --- .../app/features/variables/query/queryRunners.test.ts | 10 +++++++++- public/app/features/variables/query/queryRunners.ts | 5 +++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/public/app/features/variables/query/queryRunners.test.ts b/public/app/features/variables/query/queryRunners.test.ts index bef2104d212..3d76a655f62 100644 --- a/public/app/features/variables/query/queryRunners.test.ts +++ b/public/app/features/variables/query/queryRunners.test.ts @@ -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', () => { diff --git a/public/app/features/variables/query/queryRunners.ts b/public/app/features/variables/query/queryRunners.ts index d51cda15e06..b014d07034d 100644 --- a/public/app/features/variables/query/queryRunners.ts +++ b/public/app/features/variables/query/queryRunners.ts @@ -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; }