Skip to content

Commit 7f7e759

Browse files
charlypolyTkDodo
andauthored
doc(graphql): update "Type-Safety and Code Generation" section (#4371)
* doc(graphql): update "Type-Safety and Code Generation" section * Update docs/graphql.md Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>
1 parent 9fc79d6 commit 7f7e759

1 file changed

Lines changed: 38 additions & 3 deletions

File tree

docs/graphql.md

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,43 @@ Because React Query's fetching mechanisms are agnostically built on Promises, yo
99
1010
## Type-Safety and Code Generation
1111

12-
You can use [GraphQL-Codegen](https://graphql-code-generator.com/) to generate ready-to-use React Hooks, based on your GraphQL operations. [You can find more details and examples here](https://www.graphql-code-generator.com/docs/plugins/typescript-react-query).
1312

14-
## Examples
13+
React Query, used in combination with `graphql-request^5` and [GraphQL Code Generator](https://graphql-code-generator.com/) provides full-typed GraphQL operations:
1514

16-
- [basic-graphql-request](../examples/react/basic-graphql-request) (The "basic" example, but implemented with [`graphql-request`](https://github.com/prisma-labs/graphql-request))
15+
```tsx
16+
import request from 'graphql-request';
17+
import { useQuery } from '@tanstack/react-query';
18+
19+
import { graphql } from './gql/gql';
20+
21+
const allFilmsWithVariablesQueryDocument = graphql(/* GraphQL */ `
22+
query allFilmsWithVariablesQuery($first: Int!) {
23+
allFilms(first: $first) {
24+
edges {
25+
node {
26+
id
27+
title
28+
}
29+
}
30+
}
31+
}
32+
`);
33+
34+
function App() {
35+
// `data` is fully typed!
36+
const { data } = useQuery(['films'], async () =>
37+
request(
38+
'https://swapi-graphql.netlify.app/.netlify/functions/index',
39+
allFilmsWithVariablesQueryDocument,
40+
// variables are type-checked too!
41+
{ first: 10 }
42+
)
43+
);
44+
45+
// ...
46+
}
47+
```
48+
_You can find a [complete example in the repo](https://github.com/dotansimha/graphql-code-generator/tree/7c25c4eeb77f88677fd79da557b7b5326e3f3950/examples/front-end/react/tanstack-react-query)_
49+
50+
51+
Get started with the [dedicated guide on GraphQL Code Generator documentation](https://www.the-guild.dev/graphql/codegen/docs/guides/react-vue).

0 commit comments

Comments
 (0)