Skip to content

Commit 976b54f

Browse files
authored
Update changelog for defer support (#9455)
1 parent 80fa1c2 commit 976b54f

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

.changeset/eleven-news-lay.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,65 @@
88
---
99

1010
Add `@defer` directive support
11+
12+
When a query includes a deferred fragment field, the server will return a partial response with the non-deferred fields first, followed by the remaining fields once they have been resolved.
13+
14+
Once start using the `@defer` directive in your queries, the generated code will automatically include support for the directive.
15+
16+
```jsx
17+
// src/index.tsx
18+
import { graphql } from './gql'
19+
const OrdersFragment = graphql(`
20+
fragment OrdersFragment on User {
21+
orders {
22+
id
23+
total
24+
}
25+
}
26+
`)
27+
const GetUserQuery = graphql(`
28+
query GetUser($id: ID!) {
29+
user(id: $id) {
30+
id
31+
name
32+
...OrdersFragment @defer
33+
}
34+
}
35+
`)
36+
```
37+
38+
The generated type for `GetUserQuery` will have information that the fragment is _incremental,_ meaning it may not be available right away.
39+
40+
```tsx
41+
// gql/graphql.ts
42+
export type GetUserQuery = { __typename?: 'Query'; id: string; name: string } & ({
43+
__typename?: 'Query'
44+
} & {
45+
' $fragmentRefs'?: { OrdersFragment: Incremental<OrdersFragment> }
46+
})
47+
```
48+
49+
Apart from generating code that includes support for the `@defer` directive, the Codegen also exports a utility function called `isFragmentReady`. You can use it to conditionally render components based on whether the data for a deferred
50+
fragment is available:
51+
52+
```jsx
53+
const OrdersList = (props: { data: FragmentType<typeof OrdersFragment> }) => {
54+
const data = useFragment(OrdersFragment, props.data);
55+
return (
56+
// render orders list
57+
)
58+
};
59+
60+
function App() {
61+
const { data } = useQuery(GetUserQuery);
62+
return (
63+
{data && (
64+
<>
65+
{isFragmentReady(GetUserQuery, OrdersFragment, data)
66+
&& <OrdersList data={data} />}
67+
</>
68+
)}
69+
);
70+
}
71+
export default App;
72+
```

0 commit comments

Comments
 (0)