|
| 1 | +--- |
| 2 | +link: plugins/typedDocumentNode |
| 3 | +title: TypedDocumentNode |
| 4 | +order: 4 |
| 5 | +category: Plugins |
| 6 | +--- |
| 7 | + |
| 8 | +## Usage with Typed Document Node |
| 9 | + |
| 10 | +Zeus can generate builders for [`TypedDocumentNode`][typed-document-node], a type-safe query |
| 11 | +representation understood by most GraphQL clients (including Apollo, urql etc) by adding the |
| 12 | +`--typedDocumentNode` flag to the CLI. |
| 13 | + |
| 14 | +### Generate Type-Safe Zeus Schema And TypedDocumentNode query builders |
| 15 | + |
| 16 | +```sh |
| 17 | +$ zeus schema.graphql ./ --typedDocumentNode |
| 18 | +# typedDocumentNode.ts file with typed document node builders is now in the output destination |
| 19 | +``` |
| 20 | + |
| 21 | +### TypedDocumentNode + Apollo Client useQuery examples |
| 22 | + |
| 23 | +The following example demonstrates usage with Apollo. Other clients should work similarly. |
| 24 | + |
| 25 | +```tsx |
| 26 | +import { query, $ } from './zeus/typedDocumentNode'; |
| 27 | +import { useQuery } from '@apollo/client'; |
| 28 | + |
| 29 | +const myQuery = query({ |
| 30 | + // Get autocomplete here: |
| 31 | + cardById: [ |
| 32 | + { cardId: $('cardId') }, |
| 33 | + { |
| 34 | + Attack: true, |
| 35 | + Defense: true, |
| 36 | + }, |
| 37 | + ], |
| 38 | +}); |
| 39 | + |
| 40 | +const Main = () => { |
| 41 | + const { data } = useQuery(myQuery, { |
| 42 | + variables: { |
| 43 | + // Get autocomplete and typechecking here: |
| 44 | + cardId: someId |
| 45 | + } |
| 46 | + }); |
| 47 | + // data response is typed |
| 48 | + return <div>{data.drawCard.name}</div>; |
| 49 | +}; |
| 50 | +``` |
| 51 | + |
| 52 | +### Variable support |
| 53 | + |
| 54 | +Variables should be supported at any level of the query. Examples: |
| 55 | + |
| 56 | +```typescript |
| 57 | +const userMemberships = query({ |
| 58 | + user: [ |
| 59 | + { id: $('id') }, |
| 60 | + { memberships: [ |
| 61 | + { limit: $('limit') }, |
| 62 | + { role: true }, |
| 63 | + ], |
| 64 | + }, |
| 65 | + ], |
| 66 | +}); |
| 67 | + |
| 68 | +const mutate = mutation({ |
| 69 | + insertBooking: [ |
| 70 | + { object: $('booking') }, |
| 71 | + { id: true, bookerName: true }, |
| 72 | + ], |
| 73 | +}); |
| 74 | +``` |
| 75 | + |
| 76 | +[typed-document-node]: https://www.graphql-code-generator.com/plugins/typed-document-node |
0 commit comments