Skip to content

Commit f7baf71

Browse files
committed
Pass full variables options down to computed view variable planner functions
Computed view planners expect to be passed full options objects for each variable, not just the list of variable values, oops.
1 parent b8582bd commit f7baf71

3 files changed

Lines changed: 19 additions & 5 deletions

File tree

packages/api-client-core/src/GadgetFunctions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export interface ViewFunctionWithVariables<VariablesT, ResultT> {
104104
variables: VariablesOptions;
105105
variablesType: VariablesT;
106106
resultType: ResultT;
107-
plan(variables: VariablesT): GQLBuilderResult;
107+
plan(variables: VariablesOptions): GQLBuilderResult;
108108
}
109109

110110
export type ViewFunction<VariablesT, ResultT> = ViewFunctionWithoutVariables<ResultT> | ViewFunctionWithVariables<VariablesT, ResultT>;

packages/react/spec/useView.spec.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ describe("useView", () => {
154154
expect(client.executeQuery).toHaveBeenCalledTimes(1);
155155

156156
expect(query).toMatchInlineSnapshot(`
157-
"query echo($value: undefined) {
157+
"query echo($value: String) {
158158
echo(value: $value)
159159
}"
160160
`);
@@ -191,7 +191,7 @@ describe("useView", () => {
191191
expect(client.executeQuery).toHaveBeenCalledTimes(1);
192192

193193
expect(query).toMatchInlineSnapshot(`
194-
"query echo($value: undefined) {
194+
"query echo($value: Int) {
195195
game {
196196
echo(value: $value)
197197
}
@@ -234,7 +234,7 @@ describe("useView", () => {
234234
expect(client.executeQuery).toHaveBeenCalledTimes(1);
235235

236236
expect(query).toMatchInlineSnapshot(`
237-
"query widgetStats($inStockOnly: undefined) {
237+
"query widgetStats($inStockOnly: Boolean) {
238238
widgetStats(inStockOnly: $inStockOnly)
239239
}"
240240
`);

packages/react/src/useView.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type {
22
GQLBuilderResult,
3+
VariablesOptions,
34
ViewFunction,
45
ViewFunctionWithoutVariables,
56
ViewFunctionWithVariables,
@@ -126,7 +127,20 @@ export function useView<VariablesT, F extends ViewFunction<VariablesT, any>>(
126127
if (typeof view == "string") {
127128
return [{ query: inlineViewQuery, variables: { query: view, variables: memoizedVariables } }, ["gellyView"]];
128129
} else {
129-
return [view.plan((memoizedVariables ?? {}) as unknown as VariablesT), namespaceDataPath([view.gqlFieldName], view.namespace)];
130+
const variablesOptions: VariablesOptions = {};
131+
if ("variables" in view && memoizedVariables) {
132+
for (const [name, variable] of Object.entries(view.variables)) {
133+
const value = memoizedVariables[name as keyof typeof memoizedVariables] as unknown;
134+
if (typeof value != "undefined" && value !== null) {
135+
variablesOptions[name] = {
136+
value,
137+
...variable,
138+
};
139+
}
140+
}
141+
}
142+
143+
return [view.plan(variablesOptions), namespaceDataPath([view.gqlFieldName], view.namespace)];
130144
}
131145
}, [view, memoizedVariables]);
132146

0 commit comments

Comments
 (0)