|
| 1 | +export const metadata = { |
| 2 | + title: "Generate types with GraphQL Codegen", |
| 3 | +}; |
| 4 | + |
| 5 | +Faust.js provides built-in TypeScript support, including types for Templates, Blocks, and more. This guide will show you how to generate fully typed definitions for your custom GraphQL queries and fragments using [GraphQL Code Generator](https://the-guild.dev/graphql/codegen). |
| 6 | + |
| 7 | +> [!TIP] |
| 8 | +> If you're looking to generate [Apollo-specific fragment matching](https://www.apollographql.com/docs/react/data/fragments#using-fragments-with-unions-and-interfaces) metadata (i.e. `possibleTypes`), see the `faust generatePossibleTypes` command. However, that command does **not** produce TypeScript definitions for your queries. For typing your queries in Faust, continue reading below. |
| 9 | +
|
| 10 | +## 0. Prerequisites |
| 11 | + |
| 12 | +If you haven't already, follow the [Basic Setup](/docs/how-to/basic-setup/) steps to get Faust.js configured. Once your project is set up, add `@graphql-codegen/cli` to your project: |
| 13 | + |
| 14 | +```bash |
| 15 | +npm install -D typescript @graphql-codegen/cli |
| 16 | +``` |
| 17 | + |
| 18 | +## 1. Add Codegen Configuration |
| 19 | + |
| 20 | +In the root of your project, create a configuration file named `codegen.ts` |
| 21 | + |
| 22 | +```ts title="codegen.ts" |
| 23 | +import { CodegenConfig } from "@graphql-codegen/cli"; |
| 24 | + |
| 25 | +const config: CodegenConfig = { |
| 26 | + schema: "https://faustexample.wpengine.com/graphql", |
| 27 | + documents: ["src/**/*.{tsx,ts}"], |
| 28 | + generates: { |
| 29 | + "./src/__generated__/": { |
| 30 | + preset: "client", |
| 31 | + plugins: [], |
| 32 | + presetConfig: { |
| 33 | + gqlTagName: "gql", |
| 34 | + }, |
| 35 | + }, |
| 36 | + }, |
| 37 | + ignoreNoDocuments: true, |
| 38 | +}; |
| 39 | + |
| 40 | +export default config; |
| 41 | +``` |
| 42 | + |
| 43 | +## 2. Add Codegen Script |
| 44 | + |
| 45 | +Next, update your package.json to add a script for running the code generator: |
| 46 | + |
| 47 | +```json title="package.json" |
| 48 | +{ |
| 49 | + "scripts": { |
| 50 | + "generate:types": "graphql-codegen" |
| 51 | + } |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +Now you can run: |
| 56 | + |
| 57 | +```bash |
| 58 | +npm run generate:types |
| 59 | +``` |
| 60 | + |
| 61 | +This command will scan your `src/` folder for any GraphQL queries or fragments, then generate TypeScript types in `src/__generated__/graphql.ts`. |
| 62 | + |
| 63 | +> [!IMPORTANT] Important |
| 64 | +> Be sure to enable WPGraphQL introspection before running the `npm run generate` command since it is [disabled by default](https://www.wpgraphql.com/docs/security#introspection-disabled-by-default). |
| 65 | +
|
| 66 | +## 3. Using Generated Types |
| 67 | + |
| 68 | +### A. Typing Templates |
| 69 | + |
| 70 | +After Codegen runs, you'll see auto-generated types in graphql.ts. For example: |
| 71 | + |
| 72 | +```ts title="src/__generated__/graphql.ts" |
| 73 | +export type GetPostQueryVariables = Exact<{ |
| 74 | + databaseId: Scalars["ID"]; |
| 75 | + asPreview?: InputMaybe<Scalars["Boolean"]>; |
| 76 | +}>; |
| 77 | + |
| 78 | +export type GetPostQuery = { |
| 79 | + // ... |
| 80 | +}; |
| 81 | +``` |
| 82 | + |
| 83 | +You can use these types with the `FaustTemplate` helper in your WordPress templates: |
| 84 | + |
| 85 | +```tsx title="wp-templates/single.tsx" |
| 86 | +import { gql } from "../__generated__"; |
| 87 | +import { GetPostQuery } from "../__generated__/graphql"; |
| 88 | +import { FaustTemplate } from "@faustwp/core"; |
| 89 | + |
| 90 | +const Component: FaustTemplate<GetPostQuery> = (props) => { |
| 91 | + // `props.data` and other fields are now typed! |
| 92 | + return <div>{props?.data?.post?.title}</div>; |
| 93 | +}; |
| 94 | + |
| 95 | +export const pageQuery = gql(/* GraphQL */ ` |
| 96 | + query GetPost($databaseId: ID!, $asPreview: Boolean) { |
| 97 | + ... |
| 98 | + } |
| 99 | +`); |
| 100 | +``` |
| 101 | + |
| 102 | +Then you can inspect all the types in the `props` parameters as you type: |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | +All the data from the query results will be properly typed based on the introspected schema: |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | +### B. Typing Block Components |
| 111 | + |
| 112 | +If you create blocks with `@faustwp/blocks`, you can use the WordPressBlock type to add strong typing to those components: |
| 113 | + |
| 114 | +```tsx title="wp-blocks/CoreParagraph.tsx" |
| 115 | +import { gql } from "../__generated__"; |
| 116 | +import { WordPressBlock } from "@faustwp/blocks"; |
| 117 | +import { CoreParagraphFragmentFragment } from "../__generated__/graphql"; |
| 118 | + |
| 119 | +const CoreParagraph: WordPressBlock<CoreParagraphFragmentFragment> = ( |
| 120 | + props, |
| 121 | +) => { |
| 122 | + return <p>{props.attributes?.content}</p>; |
| 123 | +}; |
| 124 | + |
| 125 | +export const fragments = { |
| 126 | + entry: gql(` |
| 127 | + fragment CoreParagraphFragment on CoreParagraph { |
| 128 | + attributes { |
| 129 | + content |
| 130 | + } |
| 131 | + } |
| 132 | + `), |
| 133 | + key: "CoreParagraphFragment", |
| 134 | +}; |
| 135 | +``` |
| 136 | + |
| 137 | +By passing in `CoreParagraphFragmentFragment` to WordPressBlock, TypeScript enforces that props only contains fields you've declared in the GraphQL fragment. |
| 138 | + |
| 139 | +## Further Reading |
| 140 | + |
| 141 | +- [Migrating to TypeScript](/docs/explanation/migrating-to-typescript) |
| 142 | +- [TypeScript Reference](/docs/reference/typescript) |
0 commit comments