Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Build SDK and document questions from the store's builder (#81867)

Romeo Van Snickc9c25529644b
diff --git a/frontend/src/embedding-sdk-bundle/lib/sdk-question/load-question.ts b/frontend/src/embedding-sdk-bundle/lib/sdk-question/load-question.ts
index a82f9b68c86..e628374bca9 100644
--- a/frontend/src/embedding-sdk-bundle/lib/sdk-question/load-question.ts
+++ b/frontend/src/embedding-sdk-bundle/lib/sdk-question/load-question.ts
@@ -7,13 +7,16 @@ import type {
LoadSdkQuestionParams,
SdkQuestionState,
} from "embedding-sdk-bundle/types/question";
-import { getMetadata, paramFieldsFetched } from "metabase/metadata-store";
+import {
+ getMetadata,
+ paramFieldsFetched,
+ selectQuestionFromCard,
+} from "metabase/metadata-store";
import {
getParameterValuesForQuestion,
resolveCards,
} from "metabase/query_builder";
import { loadMetadataForCard } from "metabase/questions/actions";
-import Question from "metabase-lib/v1/Question";
import type { SeriesCard } from "metabase-types/api";
import type { EntityToken } from "metabase-types/api/entity";
@@ -63,13 +66,11 @@ export const loadQuestionSdk =
await dispatch(loadMetadataForCard(card, { token }));
}
- const metadata = getMetadata(getState());
-
const originalQuestion = originalCard
- ? new Question(originalCard, metadata)
+ ? selectQuestionFromCard(getState(), originalCard)
: undefined;
- let question = new Question(card, metadata);
+ let question = selectQuestionFromCard(getState(), card);
if (targetDashboardId) {
question = question.setDashboardId(targetDashboardId);
}
@@ -82,7 +83,9 @@ export const loadQuestionSdk =
const parameterValues = getParameterValuesForQuestion({
card,
- metadata,
+ // getParameterValuesForQuestion reaches getCardUiParameters, which still
+ // takes the Metadata object. It migrates when that utility does.
+ metadata: getMetadata(getState()),
queryParams: initialSqlParameters,
});
diff --git a/frontend/src/embedding-sdk-bundle/lib/sdk-question/run-question-on-navigate.ts b/frontend/src/embedding-sdk-bundle/lib/sdk-question/run-question-on-navigate.ts
index e3b1bd6a523..e3fbc9c29db 100644
--- a/frontend/src/embedding-sdk-bundle/lib/sdk-question/run-question-on-navigate.ts
+++ b/frontend/src/embedding-sdk-bundle/lib/sdk-question/run-question-on-navigate.ts
@@ -4,12 +4,12 @@ import type {
SdkQuestionState,
} from "embedding-sdk-bundle/types/question";
import { cardIsEquivalent } from "metabase/common/utils/card";
-import { getMetadata } from "metabase/metadata-store";
+import { selectQuestionFromCard } from "metabase/metadata-store";
import { loadCard } from "metabase/query_builder";
import { loadMetadataForCard } from "metabase/questions/actions";
import type { Dispatch, GetState } from "metabase/redux/store";
import { getCardAfterVisualizationClick } from "metabase/viz-core";
-import Question from "metabase-lib/v1/Question";
+import type Question from "metabase-lib/v1/Question";
import type { ParameterValuesMap } from "metabase-types/api";
import type { EntityToken } from "metabase-types/api/entity";
@@ -62,12 +62,12 @@ export const runQuestionOnNavigateSdk =
}
// Optimistic update the UI before we re-fetch the query metadata.
- onQuestionChange(new Question(nextCard, getMetadata(getState())));
+ onQuestionChange(selectQuestionFromCard(getState(), nextCard));
await dispatch(loadMetadataForCard(nextCard, { token }));
const state = await runQuestionQuerySdk({
- question: new Question(nextCard, getMetadata(getState())),
+ question: selectQuestionFromCard(getState(), nextCard),
originalQuestion,
parameterValues,
signal,
diff --git a/frontend/src/embedding-sdk-bundle/lib/sdk-question/update-question.ts b/frontend/src/embedding-sdk-bundle/lib/sdk-question/update-question.ts
index 33414a57df5..038017588fd 100644
--- a/frontend/src/embedding-sdk-bundle/lib/sdk-question/update-question.ts
+++ b/frontend/src/embedding-sdk-bundle/lib/sdk-question/update-question.ts
@@ -1,7 +1,7 @@
import _ from "underscore";
import type { SdkQuestionState } from "embedding-sdk-bundle/types/question";
-import { getMetadata } from "metabase/metadata-store";
+import { selectQuestionFromCard } from "metabase/metadata-store";
import {
computeQuestionPivotTable,
getAdHocQuestionWithVizSettings,
@@ -10,7 +10,7 @@ import { loadMetadataForCard } from "metabase/questions/actions";
import type { Dispatch, GetState } from "metabase/redux/store";
import { createRawSeries } from "metabase/viz-core";
import * as Lib from "metabase-lib";
-import Question from "metabase-lib/v1/Question";
+import type Question from "metabase-lib/v1/Question";
import type { ParameterValuesMap } from "metabase-types/api";
import type { EntityToken } from "metabase-types/api/entity";
@@ -118,10 +118,9 @@ export const updateQuestionSdk =
await dispatch(loadMetadataForCard(nextQuestion.card(), { token }));
}
- const metadata = getMetadata(getState());
- nextQuestion = new Question(
+ nextQuestion = selectQuestionFromCard(
+ getState(),
nextQuestion.card(),
- metadata,
nextParameterValues,
);
diff --git a/frontend/src/metabase/documents/components/editor-extensions/CardEmbed/use-update-card-operations.ts b/frontend/src/metabase/documents/components/editor-extensions/CardEmbed/use-update-card-operations.ts
index 6aad72eada1..9ce0c09876d 100644
--- a/frontend/src/metabase/documents/components/editor-extensions/CardEmbed/use-update-card-operations.ts
+++ b/frontend/src/metabase/documents/components/editor-extensions/CardEmbed/use-update-card-operations.ts
@@ -1,14 +1,14 @@
import type { NodeViewProps } from "@tiptap/core";
import { useCallback } from "react";
-import { getMetadata } from "metabase/metadata-store";
-import { useDispatch, useSelector } from "metabase/redux";
+import { useQuestionFromCard } from "metabase/metadata-store";
+import { useDispatch } from "metabase/redux";
import {
type UseCardDataResult,
useEditorHost,
} from "metabase/rich_text_editing/tiptap/EditorHost";
import * as Urls from "metabase/urls";
-import Question from "metabase-lib/v1/Question";
+import type Question from "metabase-lib/v1/Question";
import type {
Document,
SeriesCard,
@@ -32,7 +32,7 @@ export const useUpdateCardOperations = ({
}) => {
const host = useEditorHost();
const dispatch = useDispatch();
- const metadata = useSelector(getMetadata);
+ const buildQuestion = useQuestionFromCard();
const { card, draftCard, regularDataset } = regularCardData;
@@ -48,16 +48,11 @@ export const useUpdateCardOperations = ({
// Handle drill-through navigation
const handleChangeCardAndRun = useCallback(
({ nextCard }: { nextCard: SeriesCard }) => {
- if (!metadata) {
- console.warn("Metadata not available for drill-through navigation");
- return;
- }
-
try {
// For drill-through, we need to ensure the card is treated as adhoc
// Remove the ID so getUrl creates an adhoc question URL instead of navigating to saved question
const adhocCard = { ...nextCard, id: null };
- const question = new Question(adhocCard, metadata);
+ const question = buildQuestion(adhocCard);
const url = Urls.question(question);
dispatch(host.navigateToCard(url, document));
} catch (error) {
@@ -72,7 +67,7 @@ export const useUpdateCardOperations = ({
}
}
},
- [dispatch, host, metadata, document],
+ [dispatch, host, buildQuestion, document],
);
const handleUpdateVisualizationSettings = useCallback(