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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc(data-fetching): add mention of GraphQL Code Generator for end-to-end type safety #359

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions pages/docs/data-fetching.en-US.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,39 @@ function App () {
```

_If you want to pass variables to a GraphQL query, check out [Multiple Arguments](/docs/arguments)._

**End-to-end type safety with GraphQL Code Generator**

Using SWR and `graphql-request` along with [GraphQL Code Generator](https://www.the-guild.dev/graphql/codegen) will get end-to-end TypeScript types, inferred from the given GraphQL Query:

```ts
import request from 'graphql-request';
import { graphql } from '../gql/gql';


const getMovieQueryDocument = graphql(/* GraphQL */ `
query getMovie($title: String!) {
Movie(title: $title) {
releaseDate
actors {
name
}
}
}
`);

function App () {
// `data` is typed
const { data } = useSWR(
// `variables` are typed too
['movie', { title: "Inception" }],
async (_key, variables) =>
request('/api/graphql', getMovieQueryDocument, variables)
);

// ...
};
```
[_Complete example is available in the GraphQL Code Generator repository_](https://github.com/dotansimha/graphql-code-generator/tree/master/examples/front-end/react/swr)

Visit GraphQL Code Generator's dedicated guide to get started: https://www.the-guild.dev/graphql/codegen/docs/guides/react-vue.