Open History to see the last commit
1---
2id: graphql
3title: GraphQL
4---
5β
6Because React Query's fetching mechanisms are agnostically built on Promises, you can use React Query with literally any asynchronous data fetching client, including GraphQL!
7β
8> Keep in mind that React Query does not support normalized caching. While a vast majority of users do not actually need a normalized cache or even benefit from it as much as they believe they do, there may be very rare circumstances that may warrant it so be sure to check with us first to make sure it's truly something you need!
9β
10## Type-Safety and Code Generation
11β
12β
13React Query, used in combination with `graphql-request^5` and [GraphQL Code Generator](https://graphql-code-generator.com/) provides full-typed GraphQL operations:
14β
15```tsx
16import request from 'graphql-request';
17import { useQuery } from '@tanstack/react-query';
18β
19import { graphql } from './gql/gql';
20β
21const 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β
34function 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β
51Get started with the [dedicated guide on GraphQL Code Generator documentation](https://www.the-guild.dev/graphql/codegen/docs/guides/react-vue).