Skip to content

Commit 7c33689

Browse files
authored
Update client codegen doc to match v6 release (#13162)
This PR updates GraphQL Codegen docs for `typescript-operations` and `client-preset` v6 release: dotansimha/graphql-code-generator#10496 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Documentation** * Updated GraphQL Code Generator guidance to favor operation-only generated types and simplified starter examples. * Removed several advanced customization options and deprecated inline base-type settings in favor of importing a central generated types artifact. * Clarified client-preset and near-operation-file guidance and added a config option to avoid duplicate operation-type generation for consistency. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 0c81cb9 commit 7c33689

1 file changed

Lines changed: 168 additions & 2 deletions

File tree

docs/source/development-testing/graphql-codegen.mdx

Lines changed: 168 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,53 @@ This article covers GraphQL Codegen installation and configuration. If you'd lik
2020
Install the following packages. This installation assumes you already have [installed `@apollo/client` and its dependencies](../get-started#step-2-install-dependencies).
2121

2222
```bash
23-
npm install -D @graphql-codegen/cli @graphql-codegen/typescript @graphql-codegen/typescript-operations
23+
npm install -D @graphql-codegen/cli @graphql-codegen/typescript-operations
2424
```
2525

26+
<Note>
27+
28+
The guide below is for `@graphql-codegen/typescript-operations` v6 and `@graphql-codegen/client-preset` v6. If you are still using v5, use the configuration in at the end of each section instead.
29+
30+
</Note>
31+
2632
### Recommended starter configuration
2733

2834
Next, we'll create a configuration file for GraphQL Code Generator, named [`codegen.ts`](https://www.the-guild.dev/graphql/codegen/docs/config-reference/codegen-config), at the root of our project. The following is a recommended minimal configuration for Apollo Client apps.
2935

3036
```ts title="codegen.ts"
3137
import { CodegenConfig } from "@graphql-codegen/cli";
3238

39+
const config: CodegenConfig = {
40+
overwrite: true,
41+
schema: "<URL_OF_YOUR_GRAPHQL_API>",
42+
// This assumes that all your source files are in a top-level `src/` directory - you might need to adjust this to your file structure
43+
documents: ["src/**/*.{ts,tsx}"],
44+
// Don't exit with non-zero status when there are no documents
45+
ignoreNoDocuments: true,
46+
generates: {
47+
// Use a path that works the best for the structure of your application
48+
"./src/types/__generated__/graphql.ts": {
49+
plugins: ["typescript-operations"],
50+
config: {
51+
// Apollo Client always includes `__typename` fields
52+
nonOptionalTypename: true,
53+
// Apollo Client doesn't add the `__typename` field to root types so
54+
// don't generate a type for the `__typename` for root operation types.
55+
skipTypeNameForRoot: true,
56+
},
57+
},
58+
},
59+
};
60+
61+
export default config;
62+
```
63+
64+
<details>
65+
<summary>Still using `@graphql-codegen/typescript-operations` v5? Use this configuration instead</summary>
66+
67+
```ts title="codegen.ts"
68+
import { CodegenConfig } from "@graphql-codegen/cli";
69+
3370
const config: CodegenConfig = {
3471
overwrite: true,
3572
schema: "<URL_OF_YOUR_GRAPHQL_API>",
@@ -63,6 +100,8 @@ const config: CodegenConfig = {
63100
export default config;
64101
```
65102

103+
</details>
104+
66105
<Note>
67106

68107
There are multiple ways to [specify a schema](https://www.the-guild.dev/graphql/codegen/docs/config-reference/schema-field#root-level) in your `codegen.ts`. Use the method that best fits your project's needs.
@@ -91,13 +130,48 @@ $ npm run codegen
91130

92131
If you follow GraphQL Codegen's [quickstart guide](https://the-guild.dev/graphql/codegen/docs/getting-started/installation), it recommends generating your config file using the GraphQL Code Generator CLI. This wizard installs and configures the [`@graphql-codegen/client-preset`](https://the-guild.dev/graphql/codegen/plugins/presets/preset-client).
93132

94-
We do not recommend using the client preset with Apollo Client apps because it generates additional runtime code that adds bundle size to your application and includes features that are incompatible with Apollo Client. Instead, we recommend using the [`typescript`](https://the-guild.dev/graphql/codegen/plugins/typescript/typescript) and [`typescript-operations`](https://the-guild.dev/graphql/codegen/plugins/typescript/typescript-operations) plugins directly (at minimum), which focus on only generating types and don't include additional runtime code. Follow the steps in the preceding section to use a setup that includes these plugins.
133+
We do not recommend using the client preset with Apollo Client apps because it generates additional runtime code that adds bundle size to your application and includes features that are incompatible with Apollo Client. Instead, we recommend using the [`typescript-operations`](https://the-guild.dev/graphql/codegen/plugins/typescript/typescript-operations) plugin directly (at minimum), which focuses on only generating types and doesn't include additional runtime code. Follow the steps in the preceding section to use a setup that includes this plugin.
95134

96135
If you're already using the client preset, or you choose to use it instead of working directly with the plugins, we recommend the following minimal configuration for Apollo Client apps.
97136

98137
```ts
99138
import { CodegenConfig } from "@graphql-codegen/cli";
100139

140+
const config: CodegenConfig = {
141+
overwrite: true,
142+
schema: "<URL_OF_YOUR_GRAPHQL_API>",
143+
// This assumes that all your source files are in a top-level `src/` directory - you might need to adjust this to your file structure
144+
documents: ["src/**/*.{ts,tsx}", "!src/gql/**/*"],
145+
// Don't exit with non-zero status when there are no documents
146+
ignoreNoDocuments: true,
147+
generates: {
148+
// Use a path that works the best for the structure of your application
149+
"./src/gql/": {
150+
preset: "client",
151+
presetConfig: {
152+
// Disable fragment masking
153+
fragmentMasking: false,
154+
},
155+
config: {
156+
// Apollo Client always includes `__typename` fields
157+
nonOptionalTypename: true,
158+
// Apollo Client doesn't add the `__typename` field to root types so
159+
// don't generate a type for the `__typename` for root operation types.
160+
skipTypeNameForRoot: true,
161+
},
162+
},
163+
},
164+
};
165+
166+
export default config;
167+
```
168+
169+
<details>
170+
<summary>Still using `@graphql-codegen/client-preset` v5? Use this configuration instead</summary>
171+
172+
```ts
173+
import { CodegenConfig } from "@graphql-codegen/cli";
174+
101175
const config: CodegenConfig = {
102176
overwrite: true,
103177
schema: "<URL_OF_YOUR_GRAPHQL_API>",
@@ -135,6 +209,8 @@ const config: CodegenConfig = {
135209
export default config;
136210
```
137211

212+
</details>
213+
138214
<Caution>
139215

140216
This configuration [disables fragment masking](https://the-guild.dev/graphql/codegen/plugins/presets/preset-client#how-to-disable-fragment-masking) in the client preset because it's incompatible with Apollo Client's data masking functionality. If you are using the generated `useFragment` function or anything generated from the fragment masking feature, you need to migrate away from it to use data masking in Apollo Client. See the guide on [data masking](../data/fragments#data-masking) for more information on using TypeScript with Apollo Client's data masking feature, including instructions on how to migrate away from GraphQL Codegen's fragment masking feature.
@@ -156,6 +232,49 @@ The following is a recommended minimal configuration. See the [`near-operation-f
156232
```ts title="codegen.ts"
157233
import { CodegenConfig } from "@graphql-codegen/cli";
158234

235+
const config: CodegenConfig = {
236+
overwrite: true,
237+
schema: "<URL_OF_YOUR_GRAPHQL_API>",
238+
// This assumes that all your source files are in a top-level `src/` directory - you might need to adjust this to your file structure
239+
documents: ["src/**/*.{ts,tsx}"],
240+
// Don't exit with non-zero status when there are no documents
241+
ignoreNoDocuments: true,
242+
generates: {
243+
"./src/types/__generated__/graphql.ts": {
244+
plugins: ["typescript-operations"],
245+
config: {
246+
// Generates the enum and input types used in operation variables.
247+
// The files generated from the config below import these types into near-operation files.
248+
generateOperationTypes: false,
249+
},
250+
},
251+
"./src/": {
252+
preset: "near-operation-file",
253+
plugins: ["typescript-operations"],
254+
// Note: these config options moved from the other generated file config
255+
config: {
256+
// This is the file generated by the `typescript-operations` plugin above,
257+
// relative to the current working directory.
258+
importSchemaTypesFrom: "./src/types/__generated__/graphql.ts",
259+
// Apollo Client always includes `__typename` fields
260+
nonOptionalTypename: true,
261+
// Apollo Client doesn't add the `__typename` field to root types so
262+
// don't generate a type for the `__typename` for root operation types.
263+
skipTypeNameForRoot: true,
264+
},
265+
},
266+
},
267+
};
268+
269+
export default config;
270+
```
271+
272+
<details>
273+
<summary>Still using `@graphql-codegen/typescript-operations` v5? Use this configuration instead</summary>
274+
275+
```ts title="codegen.ts"
276+
import { CodegenConfig } from "@graphql-codegen/cli";
277+
159278
const config: CodegenConfig = {
160279
overwrite: true,
161280
schema: "<URL_OF_YOUR_GRAPHQL_API>",
@@ -198,6 +317,8 @@ const config: CodegenConfig = {
198317
export default config;
199318
```
200319

320+
</details>
321+
201322
#### Usage
202323

203324
The following example imports the generated types relative to the current file.
@@ -249,6 +370,49 @@ The following is a recommended minimal configuration which uses the `near-operat
249370
```ts {21} title="codegen.ts"
250371
import { CodegenConfig } from "@graphql-codegen/cli";
251372

373+
const config: CodegenConfig = {
374+
overwrite: true,
375+
schema: "<URL_OF_YOUR_GRAPHQL_API>",
376+
// This assumes that all your source files are in a top-level `src/` directory - you might need to adjust this to your file structure
377+
documents: ["src/**/*.{ts,tsx}"],
378+
// Don't exit with non-zero status when there are no documents
379+
ignoreNoDocuments: true,
380+
generates: {
381+
"./src/types/__generated__/graphql.ts": {
382+
plugins: ["typescript-operations"],
383+
config: {
384+
// Generates the enum and input types used in operation variables.
385+
// The files generated from the config below import these types into near-operation files.
386+
generateOperationTypes: false,
387+
},
388+
},
389+
"./src/": {
390+
preset: "near-operation-file",
391+
plugins: ["typescript-operations", "typed-document-node"],
392+
// Note: these config options moved from the other generated file config
393+
config: {
394+
// This is the file generated by the `typescript-operations` plugin above,
395+
// relative to the current working directory.
396+
importSchemaTypesFrom: "./src/types/__generated__/graphql.ts",
397+
// Apollo Client always includes `__typename` fields
398+
nonOptionalTypename: true,
399+
// Apollo Client doesn't add the `__typename` field to root types so
400+
// don't generate a type for the `__typename` for root operation types.
401+
skipTypeNameForRoot: true,
402+
},
403+
},
404+
},
405+
};
406+
407+
export default config;
408+
```
409+
410+
<details>
411+
<summary>Still using `@graphql-codegen/typescript-operations` v5? Use this configuration instead</summary>
412+
413+
```ts {21} title="codegen.ts"
414+
import { CodegenConfig } from "@graphql-codegen/cli";
415+
252416
const config: CodegenConfig = {
253417
overwrite: true,
254418
schema: "<URL_OF_YOUR_GRAPHQL_API>",
@@ -291,6 +455,8 @@ const config: CodegenConfig = {
291455
export default config;
292456
```
293457

458+
</details>
459+
294460
<Note>
295461

296462
You might need to change the structure of your application to avoid bundling the query more than once in your application. If you author GraphQL documents using the `gql` template tag, you won't consume the returned GraphQL document since you'll import and use the precompiled document generated by this plugin instead.<br/><br/>

0 commit comments

Comments
 (0)