Skip to content

Commit 8f0250f

Browse files
authored
docs(graphql): stabilize query context references (#608)
* docs(graphql): stabilize query context references * docs(graphql): clarify hook scope in query context example * docs(graphql): explain why query context is hoisted and mutation context is not Fix the dangling colon before the example, state that useQuery lists context as a hook dependency while the mutation second argument is per-call, and restore the query/mutation side comments.
1 parent 1402b6f commit 8f0250f

4 files changed

Lines changed: 49 additions & 31 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@tailor-platform/app-shell": patch
3+
---
4+
5+
Keep the GraphQL documentation and skill example's query context stable between renders.
6+
This avoids urql query re-execution loops when the example is used in a component.

‎docs-manifest.json‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -795,9 +795,9 @@
795795
"symbols": [],
796796
"hashes": {
797797
"typeSurface": null,
798-
"outline": "c75d2a9a871756e6",
798+
"outline": "37eafa8a70959a4d",
799799
"snapshot": null,
800-
"outputMd": "ba5107604e4f42e8",
800+
"outputMd": "a59a10293fba053b",
801801
"examples": null
802802
}
803803
},
@@ -1673,7 +1673,7 @@
16731673
"packages/core/skills/app-shell-patterns/references/concepts/authentication.md": "71f5f42c0fa0698e",
16741674
"packages/core/skills/app-shell-patterns/references/concepts/custom-components.md": "18b8fc747d52455a",
16751675
"packages/core/skills/app-shell-patterns/references/concepts/file-based-routing.md": "e189f95dbe4b0286",
1676-
"packages/core/skills/app-shell-patterns/references/concepts/graphql.md": "ba5107604e4f42e8",
1676+
"packages/core/skills/app-shell-patterns/references/concepts/graphql.md": "a59a10293fba053b",
16771677
"packages/core/skills/app-shell-patterns/references/concepts/modules-and-resources.md": "bf15b5460f22d165",
16781678
"packages/core/skills/app-shell-patterns/references/concepts/routing-navigation.md": "8034c7f885994b29",
16791679
"packages/core/skills/app-shell-patterns/references/concepts/sidebar-navigation.md": "e342ef02dc2b9b86",

‎docs-src/concepts/graphql.docs.outline.md‎

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -153,22 +153,28 @@ function CreateOrderButton() {
153153

154154
### `additionalTypenames`
155155

156-
When a mutation creates/updates/deletes an entity that another query already has cached, urql won't know to refetch unless you pass `additionalTypenames`. Use it on **both** the query and the mutation when they share a typename:
156+
When a mutation creates/updates/deletes an entity that another query already has cached, urql won't know to refetch unless you pass `additionalTypenames`. Use it on **both** the query and the mutation when they share a typename.
157+
158+
Define the query's static `context` at module scope, outside the component, so its reference stays stable between renders. `useQuery` lists `context` as a hook dependency, so a fresh object on each render re-creates the query subscription and can cause query re-execution loops. The mutation's second argument is passed per call and is not a hook dependency, so it can stay inline.
157159

158160
```tsx
159-
// Query side — declares it cares about the Order typename
160-
const [{ data }] = useQuery({
161-
query: OrdersQuery,
162-
variables: { first: 25 },
163-
context: { additionalTypenames: ["Order"] },
164-
});
165-
166-
// Mutation side — declares it produces Order writes
167-
const [, createOrder] = useMutation(CreateOrderMutation);
168-
await createOrder(
169-
{ input },
170-
{ additionalTypenames: ["Order"] }, // triggers refetch of any query subscribed to 'Order'
171-
);
161+
const ordersQueryContext = { additionalTypenames: ["Order"] };
162+
163+
function useOrders() {
164+
// Query side — declares it cares about the Order typename
165+
const [{ data }] = useQuery({
166+
query: OrdersQuery,
167+
variables: { first: 25 },
168+
context: ordersQueryContext,
169+
});
170+
171+
// Mutation side — declares it produces Order writes
172+
const [, executeCreateOrder] = useMutation(CreateOrderMutation);
173+
const createOrder = (input: CreateOrderInput) =>
174+
executeCreateOrder({ input }, { additionalTypenames: ["Order"] });
175+
176+
return { data, createOrder };
177+
}
172178
```
173179

174180
Skip `additionalTypenames` only when the mutation's return type already includes the affected entity _and_ every cached query for it touched the same `id` — urql will then refresh by id. When in doubt, add it.

‎docs/concepts/graphql.md‎

Lines changed: 20 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)