Skip to content

Commit a866c7c

Browse files
authored
docs: Update variable support documentation (#2764)
1 parent 931e1e1 commit a866c7c

1 file changed

Lines changed: 70 additions & 6 deletions

File tree

docusaurus/docs/how-to-guides/data-source-plugins/add-support-for-variables.md

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,23 @@ Grafana queries your data source whenever you update a variable. Excessive updat
105105

106106
A [query variable](https://grafana.com/docs/grafana/latest/dashboards/variables/add-template-variables#add-a-query-variable) is a type of variable that allows you to query a data source for the values. By adding support for query variables to your data source plugin, users can create dynamic dashboards based on data from your data source.
107107

108+
There are two ways to do this:
109+
110+
- Implement the `metricFindQuery` method on your `DataSourceApi` class. This is the simplest option, but it's limited:
111+
- It only works with Grafana's built-in string query input, so you can't render a custom variable query editor with it alone.
112+
- The query is passed as a plain `string`, so you don't get the type safety or editor reuse of a typed query model.
113+
- It returns a `Promise`, so it can't stream results the way an `Observable`-based query can.
114+
- Assign a variable support class to the `variables` property of your data source. Extend one of the classes exported from `@grafana/data` when you want a custom or reusable query editor, typed query models, or streaming responses. Refer to [Choose a variable support class](#choose-a-variable-support-class).
115+
116+
This guide uses both: it implements `metricFindQuery` for the lookup and wraps it in a `CustomVariableSupport` class to provide a custom editor.
117+
108118
To add support for query variables, you need to:
109119

110-
1. Define a variable query model
111-
2. Implement `metricFindQuery` in your data source
112-
3. Create a `VariableQueryEditor` component
113-
4. Create a `VariableSupport` class
114-
5. Assign the `VariableSupport` to your data source
120+
1. Define a variable query model.
121+
2. Implement `metricFindQuery` in your data source.
122+
3. Create a `VariableQueryEditor` component.
123+
4. Create a variable support class. This guide extends `CustomVariableSupport`, but you can extend `DataSourceVariableSupport` or `StandardVariableSupport` instead. Refer to [Choose a variable support class](#choose-a-variable-support-class).
124+
5. Assign the variable support class to your data source.
115125

116126
Let's start by defining a query model for the variable query:
117127

@@ -294,7 +304,61 @@ export class DataSource extends DataSourceApi<MyQuery> {
294304
}
295305
```
296306

297-
That's it! You can now try out the plugin by adding a [query variable](https://grafana.com/docs/grafana/latest/dashboards/variables/add-template-variables#add-a-query-variable) to your dashboard.
307+
That's it! Now you can try out the plugin by adding a [query variable](https://grafana.com/docs/grafana/latest/dashboards/variables/add-template-variables#add-a-query-variable) to your dashboard.
308+
For a working reference implementation, see how the Grafana built-in TestData data source implements variable support in [variables.ts](https://github.com/grafana/grafana/blob/main/public/app/plugins/datasource/grafana-testdata-datasource/variables.ts).
309+
310+
#### Choose a variable support class
311+
312+
The previous steps use `CustomVariableSupport`, but `@grafana/data` exports three base classes you can assign to the `variables` property. Choose the one that matches the editor experience you want:
313+
314+
| Class | Use it when |
315+
| --- | --- |
316+
| `CustomVariableSupport` | You want to provide your own query editor component for variables. |
317+
| `DataSourceVariableSupport` | Your data source's main query editor already works for variable queries. |
318+
| `StandardVariableSupport` | You want to reuse Grafana's standard variable query editor. |
319+
320+
##### `CustomVariableSupport`
321+
322+
Provide an `editor` component and a `query` method that returns an `Observable`, as shown in the [steps above](#create-a-variablesupport-class).
323+
324+
##### `DataSourceVariableSupport`
325+
326+
Grafana reuses your existing query editor and `query` method, so there's nothing extra to implement:
327+
328+
```ts title="src/variableSupport.ts"
329+
import { DataSourceVariableSupport } from '@grafana/data';
330+
import { DataSource } from './datasource';
331+
import { MyQuery, MyDataSourceOptions } from './types';
332+
333+
export class MyVariableSupport extends DataSourceVariableSupport<DataSource, MyQuery, MyDataSourceOptions> {}
334+
```
335+
336+
##### `StandardVariableSupport`
337+
338+
Implement `toDataQuery` to convert the `StandardVariableQuery` produced by the standard editor into your data source's own query type:
339+
340+
```ts title="src/variableSupport.ts"
341+
import { StandardVariableSupport, StandardVariableQuery } from '@grafana/data';
342+
import { DataSource } from './datasource';
343+
import { MyQuery } from './types';
344+
345+
export class MyVariableSupport extends StandardVariableSupport<DataSource, MyQuery> {
346+
toDataQuery(query: StandardVariableQuery): MyQuery {
347+
return {
348+
refId: query.refId,
349+
rawQuery: query.query,
350+
};
351+
}
352+
}
353+
```
354+
355+
`StandardVariableQuery` is the query model used by the standard editor. It extends `DataQuery` and adds a single `query` string:
356+
357+
```ts
358+
interface StandardVariableQuery extends DataQuery {
359+
query: string;
360+
}
361+
```
298362

299363
## Using template variables
300364

0 commit comments

Comments
 (0)