diff --git a/packages/grafana-runtime/src/index.ts b/packages/grafana-runtime/src/index.ts index bc09e8a47f9..6e1104c92c2 100644 --- a/packages/grafana-runtime/src/index.ts +++ b/packages/grafana-runtime/src/index.ts @@ -28,7 +28,13 @@ export { export { PanelRenderer, type PanelRendererProps } from './components/PanelRenderer'; export { PanelDataErrorView, type PanelDataErrorViewProps } from './components/PanelDataErrorView'; export { toDataQueryError } from './utils/toDataQueryError'; -export { setQueryRunnerFactory, createQueryRunner, type QueryRunnerFactory } from './services/QueryRunner'; +export { + setQueryRunnerFactory, + createQueryRunner, + type QueryRunnerFactory, + setRunRequest, + getRunRequest, +} from './services/QueryRunner'; export { PluginPage } from './components/PluginPage'; export type { PluginPageType, PluginPageProps } from './components/PluginPage'; export { diff --git a/packages/grafana-runtime/src/services/QueryRunner.ts b/packages/grafana-runtime/src/services/QueryRunner.ts index 8ce5b100e7c..ce472067bfe 100644 --- a/packages/grafana-runtime/src/services/QueryRunner.ts +++ b/packages/grafana-runtime/src/services/QueryRunner.ts @@ -1,4 +1,6 @@ -import { QueryRunner } from '@grafana/data'; +import { Observable } from 'rxjs'; + +import { DataQueryRequest, DataSourceApi, PanelData, QueryRunner } from '@grafana/data'; let factory: QueryRunnerFactory | undefined; @@ -31,3 +33,30 @@ export const createQueryRunner = (): QueryRunner => { } return factory(); }; + +type RunRequestFn = ( + datasource: DataSourceApi, + request: DataQueryRequest, + queryFunction?: typeof datasource.query +) => Observable; + +let runRequest: RunRequestFn | undefined; + +/** + * Used to exspose runRequest implementation to libraries, i.e. @grafana/scenes + * + * @internal + */ +export function setRunRequest(fn: RunRequestFn): void { + if (runRequest) { + throw new Error('runRequest function should only be set once, when Grafana is starting.'); + } + runRequest = fn; +} + +export function getRunRequest(): RunRequestFn { + if (!runRequest) { + throw new Error('getRunRequest can only be used after Grafana instance has started.'); + } + return runRequest; +} diff --git a/public/app/app.ts b/public/app/app.ts index cdf836281f2..1463dccfad7 100644 --- a/public/app/app.ts +++ b/public/app/app.ts @@ -31,6 +31,7 @@ import { setEchoSrv, setLocationSrv, setQueryRunnerFactory, + setRunRequest, } from '@grafana/runtime'; import { setPanelDataErrorView } from '@grafana/runtime/src/components/PanelDataErrorView'; import { setPanelRenderer } from '@grafana/runtime/src/components/PanelRenderer'; @@ -69,6 +70,7 @@ import { PanelRenderer } from './features/panel/components/PanelRenderer'; import { DatasourceSrv } from './features/plugins/datasource_srv'; import { preloadPlugins } from './features/plugins/pluginPreloader'; import { QueryRunner } from './features/query/state/QueryRunner'; +import { runRequest } from './features/query/state/runRequest'; import { initWindowRuntime } from './features/runtime/init'; import { variableAdapters } from './features/variables/adapters'; import { createAdHocVariableAdapter } from './features/variables/adhoc/adapter'; @@ -140,6 +142,9 @@ export class GrafanaApp { setQueryRunnerFactory(() => new QueryRunner()); setVariableQueryRunner(new VariableQueryRunner()); + // Provide runRequest implementation to packages, @grafana/scenes in particular + setRunRequest(runRequest); + locationUtil.initialize({ config, getTimeRangeForUrl: getTimeSrv().timeRangeForUrl,