Skip to content

fix: replace deprecated templateSrv.getAdhocFilters usage#129

Draft
samjewell wants to merge 4 commits into
mainfrom
sj/fix-127-adhoc-filters-deprecation
Draft

fix: replace deprecated templateSrv.getAdhocFilters usage#129
samjewell wants to merge 4 commits into
mainfrom
sj/fix-127-adhoc-filters-deprecation

Conversation

@samjewell

@samjewell samjewell commented Feb 13, 2026

Copy link
Copy Markdown
Collaborator

BLOCKED ⚠️

See grafana/grafana#75552 (comment)

The SQL Preview can't show the WHERE clauses any more 😢

But I personally believe it's valuable to show these to our end users. Let's wait and see what Torkel and Dominik say.

Summary

  • Fixes Replace deprecated templateSrv.getAdhocFilters usage #127
  • replace deprecated runtime calls to templateSrv.getAdhocFilters in datasource execution paths
  • switch applyTemplateVariables to consume request-provided AdHoc filters and keep operator mapping typed via Operator
  • update unit tests to validate the new filter flow and remove SQL preview expectations that relied on deprecated template service access

Test plan

  • npm run typecheck
  • npm run test:ci -- src/datasource.test.ts
  • npm run test:ci -- src/components/QueryEditor.test.tsx

Note

Medium Risk
Touches query construction and filter/operator mapping, so regressions could change what data is returned for dashboards using AdHoc filters or template variables.

Overview
Updates AdHoc filter handling to avoid deprecated templateSrv.getAdhocFilters() in backend execution: DataSource.applyTemplateVariables and buildCubeQueryJson now accept request-provided AdHoc filters as an explicit argument and merge them into Cube filters.

Tightens operator typing by returning Operator values from mapOperator, and updates unit tests to pass AdHoc filters directly into applyTemplateVariables (simplifying template service mocks) while keeping SQL preview query building aware of AdHoc filters.

Written by Cursor Bugbot for commit 03db831. This will update automatically on new commits. Configure here.

@github-actions

github-actions Bot commented Feb 13, 2026

Copy link
Copy Markdown

Bundle Size Changes

Hello! 👋 This comment was generated by a Github Action to help you and reviewers understand the impact of your PR on frontend bundle sizes.

Whenever this PR is updated, this comment will update to reflect the latest changes.

EntryPoint Size % Diff
module 146.17 KB (-9 Bytes) -0.01%
Files Total bundle size % Diff
7 415.97 KB (-9 Bytes) 0.00%
View detailed bundle information

Added

No assets were added

Removed

No assets were removed

Bigger

No assets were bigger

Smaller

Name Size % Diff
module.js 146.17 KB (-9 Bytes) -0.01%
View module information

Added

No modules were added

Removed

No modules were removed

Bigger

Name Size % Diff
./components/QueryEditor.tsx 6.39 KB (+233 Bytes) +3.69%
./datasource.ts 10.96 KB (+195 Bytes) +1.77%
./utils/buildCubeQuery.ts 5.36 KB (+8 Bytes) +0.15%

Smaller

No modules were smaller

Comment thread src/utils/buildCubeQuery.ts
Use request-provided AdHoc filters in datasource template application and stop reading deprecated templateSrv.getAdhocFilters in runtime code paths to avoid Grafana deprecation warnings.

Co-authored-by: Cursor <cursoragent@cursor.com>
@samjewell samjewell force-pushed the sj/fix-127-adhoc-filters-deprecation branch from 55bb1a6 to a3c8195 Compare February 13, 2026 12:15

@samjewell samjewell left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tested this locally, and it works fine ✅

Pass dashboard AdHoc filters from QueryEditor into buildCubeQueryJson so SQL preview reflects the same filter context as runtime queries. Update SQL preview tests to assert merged query and dashboard filter behavior.

Co-authored-by: Cursor <cursoragent@cursor.com>

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 2 potential issues.

Bugbot Autofix is ON. A Cloud Agent has been kicked off to fix the reported issues.

Comment thread src/components/QueryEditor.tsx
Comment thread src/components/QueryEditor.tsx
@cursor

cursor Bot commented Feb 13, 2026

Copy link
Copy Markdown

Bugbot Autofix prepared fixes for 2 of the 2 bugs found in the latest run.

  • ✅ Fixed: AdHoc filters become stale in SQL preview
    • Moved AdHoc filter fetching inside the cubeQueryJson useMemo callback so filters are re-read whenever query or datasource changes, preventing staleness from the narrower datasource.name-only dependency.
  • ✅ Fixed: Missing AdHoc API guard causes editor crash
    • Added optional chaining (getAdhocFilters?.) and a fallback to empty array (?? []) to prevent TypeError when the deprecated API is unavailable.

Create PR

Or push these changes by commenting:

@cursor push 1749ae53dc
Preview (1749ae53dc)
diff --git a/src/components/QueryEditor.tsx b/src/components/QueryEditor.tsx
--- a/src/components/QueryEditor.tsx
+++ b/src/components/QueryEditor.tsx
@@ -19,10 +19,11 @@
   datasource,
 }: QueryEditorProps<DataSource, CubeQuery, CubeDataSourceOptions>) {
   const styles = useStyles2(getStyles);
-  const adHocFilters = useMemo(() => {
-    return getTemplateSrv().getAdhocFilters(datasource.name) as NonNullable<Parameters<typeof buildCubeQueryJson>[2]>;
-  }, [datasource.name]);
-  const cubeQueryJson = useMemo(() => buildCubeQueryJson(query, datasource, adHocFilters), [query, datasource, adHocFilters]);
+  const cubeQueryJson = useMemo(() => {
+    const templateSrv = getTemplateSrv();
+    const adHocFilters = (templateSrv.getAdhocFilters?.(datasource.name) ?? []) as NonNullable<Parameters<typeof buildCubeQueryJson>[2]>;
+    return buildCubeQueryJson(query, datasource, adHocFilters);
+  }, [query, datasource]);
 
   const { data, isLoading: metadataIsLoading, isError: metadataIsError } = useMetadataQuery({ datasource });
   const metadata = data ?? { dimensions: [], measures: [] };

Restore prior SQL preview test names and compact assertions while preserving coverage for AdHoc filter inclusion and merged filter behavior.

Co-authored-by: Cursor <cursoragent@cursor.com>
@samjewell samjewell force-pushed the sj/fix-127-adhoc-filters-deprecation branch from d2d1e20 to 0a54c69 Compare February 13, 2026 12:40
Remove obsolete getAdhocFilters scaffolding and centralize the default templateSrv replace mock in applyTemplateVariables tests to reduce duplication.

Co-authored-by: Cursor <cursoragent@cursor.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Replace deprecated templateSrv.getAdhocFilters usage

1 participant