Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc(graphql): update "Type-Safety and Code Generation" section #4371

Merged
merged 2 commits into from
Oct 29, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 38 additions & 3 deletions docs/graphql.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,43 @@ Because React Query's fetching mechanisms are agnostically built on Promises, yo

## Type-Safety and Code Generation

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).

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

- [basic-graphql-request](../examples/react/basic-graphql-request) (The "basic" example, but implemented with [`graphql-request`](https://github.com/prisma-labs/graphql-request))
```tsx
import request from 'graphql-request';
import { useQuery } from '@tanstack/react-query';

import { graphql } from './gql/gql';

const allFilmsWithVariablesQueryDocument = graphql(/* GraphQL */ `
query allFilmsWithVariablesQuery($first: Int!) {
allFilms(first: $first) {
edges {
node {
id
title
}
}
}
}
`);

function App() {
// `data` is fully typed!
const { data } = useQuery(['films'], async () =>
request(
'https://swapi-graphql.netlify.app/.netlify/functions/index',
allFilmsWithVariablesQueryDocument,
// variables are type-checked too!
{ first: 10 }
)
);

// ...
}
```
_You can find a [complete example in the repo](https://github.com/dotansimha/graphql-code-generator/tree/master/examples/front-end/react/tanstack-react-query)_
TkDodo marked this conversation as resolved.
Show resolved Hide resolved


Get started with the [dedicated guide on GraphQL Code Generator documentation](https://www.the-guild.dev/graphql/codegen/docs/guides/react-vue).