Skip to content

Commit 7f0bd94

Browse files
committed
Make useView hook pass the list of referenced typenames to urql
1 parent 2432079 commit 7f0bd94

File tree

4 files changed

+16
-1
lines changed

4 files changed

+16
-1
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ export interface ViewFunctionWithoutVariables<ResultT> {
8989
operationName: string;
9090
gqlFieldName: string;
9191
namespace?: string | string[] | null;
92+
referencedTypenames?: string[];
9293
resultType: ResultT;
9394
plan(): GQLBuilderResult;
9495
}
@@ -99,6 +100,7 @@ export interface ViewFunctionWithVariables<VariablesT, ResultT> {
99100
operationName: string;
100101
gqlFieldName: string;
101102
namespace?: string | string[] | null;
103+
referencedTypenames?: string[];
102104
variables: VariablesOptions;
103105
variablesType: VariablesT;
104106
resultType: ResultT;

packages/react/.changeset/silent-starfishes-cough.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
"@gadgetinc/react": minor
3+
"@gadgetinc/api-client-core": patch
34
---
45

56
Add `useView` hook for executing computed views

packages/react/spec/useView.spec.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ describe("useView", () => {
116116
expect(result.current[0].error).toBeFalsy();
117117

118118
expect(client.executeQuery).toHaveBeenCalledTimes(1);
119+
expect(client.executeQuery.mock.calls[0][1].additionalTypenames).toEqual(["Widget"]);
119120

120121
expect(query).toMatchInlineSnapshot(`
121122
"query totalInStock {

packages/react/src/useView.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,18 @@ export function useView<VariablesT, F extends ViewFunction<VariablesT, any>>(
110110
}
111111

112112
const memoizedVariables = useStructuralMemo(variables);
113-
const memoizedOptions = useStructuralMemo(options);
113+
const memoizedOptions = useStructuralMemo({
114+
...options,
115+
context: {
116+
...options?.context,
117+
// if the view exports the typenames it references, add them to the context so urql will refresh the view when mutations are made against these typenames
118+
additionalTypenames: [
119+
...(options?.context?.additionalTypenames ?? []),
120+
...(typeof view == "string" ? [] : view.referencedTypenames ?? []),
121+
],
122+
},
123+
});
124+
114125
const [plan, dataPath] = useMemo((): [plan: GQLBuilderResult, dataPath: string[]] => {
115126
if (typeof view == "string") {
116127
return [{ query: inlineViewQuery, variables: { query: view, variables: memoizedVariables } }, ["gellyView"]];

0 commit comments

Comments
 (0)