11---
22description :
3- Migrating from Apollo Tooling (apollo client:codegen) to GraphQL Code Generator. What has changed?
4- How to migrate? What configuration options replace Apollo Tooling 's behaviour?
3+ Migrating from Apollo-tooling (apollo client:codegen) to GraphQL Code Generator. What has changed?
4+ How to migrate? What configuration options replace apollo-tooling 's behaviour?
55---
66
77import { Callout , Tabs } from ' @theguild/components'
88
9- # Migrating from Apollo Tooling to GraphQL Code Generator
9+ # Migrating from Apollo-tooling to GraphQL Code Generator
1010
11- [ Apollo Tooling ] ( https://github.com/apollographql/apollo-tooling ) (the ` apollo ` CLI, specifically
11+ [ Apollo-tooling ] ( https://github.com/apollographql/apollo-tooling ) (the ` apollo ` CLI, specifically
1212` apollo client:codegen ` ) is a code generation tool that generates TypeScript types from GraphQL
1313operations for use with Apollo Client.
1414
@@ -22,7 +22,7 @@ of GraphQL Codegen.
2222
2323## Installation
2424
25- Remove Apollo Tooling and install GraphQL Code Generator:
25+ Remove apollo-tooling and install GraphQL Code Generator:
2626
2727<Tabs items = { [' Before' , ' After' ]} >
2828<Tabs.Tab >
@@ -50,19 +50,19 @@ npm i -D @graphql-codegen/cli @graphql-codegen/typescript-operations @graphql-co
5050{
5151 "devDependencies" : {
5252 ...
53- "@graphql-codegen/cli" : " 7.0.1 " ,
54- "@graphql-codegen/near-operation-file-preset" : " 5.2.0" ,
55- "@graphql-codegen/typescript-operations" : " 6.0.0" ,
53+ "@graphql-codegen/cli" : " ^ 7.0.0 " ,
54+ "@graphql-codegen/near-operation-file-preset" : " ^ 5.2.0" ,
55+ "@graphql-codegen/typescript-operations" : " ^ 6.0.0" ,
5656 ...
5757 }
5858}
5959```
6060
6161## Configuration
6262
63- Apollo Tooling is configured via ` apollo.config.js ` (or ` apollo.config.ts ` ) and invoked with
63+ Apollo-tooling is configured via ` apollo.config.js ` (or ` apollo.config.ts ` ) and invoked with
6464` apollo client:codegen ` . GraphQL Code Generator uses a ` codegen.ts ` file and is invoked with
65- ` graphql-codegen ` .
65+ ` graphql-codegen ` cli command or programmatically .
6666
6767<Tabs items = { [' Before' , ' After' ]} >
6868<Tabs.Tab >
@@ -118,9 +118,9 @@ export default config
118118
119119## Per-operation file generation with ` near-operation-file `
120120
121- Apollo Tooling 's default behaviour is to generate one TypeScript file per graphql operation, placed
121+ Apollo-tooling 's default behaviour is to generate one TypeScript file per graphql operation, placed
122122in a ` __generated__ ` folder next to the source. For example, given ` src/Component.ts ` containing a
123- query ` GetUser ` , Apollo Tooling produces ` src/__generated__/GetUser.ts ` .
123+ query ` GetUser ` , apollo-tooling produces ` src/__generated__/GetUser.ts ` .
124124
125125GraphQL Code Generator replicates this with the ` filePerOperation: true ` parameter of the
126126[ ` near-operation-file ` preset] ( https://the-guild.dev/graphql/codegen/plugins/presets/near-operation-file-preset ) .
@@ -136,7 +136,7 @@ const config: CodegenConfig = {
136136 presetConfig: {
137137 extension: ' .ts' ,
138138 folder: ' __generated__' ,
139- filePerOperation: true // Generate type files per-operation (not per-component )
139+ filePerOperation: true // Generate type files per-operation (not per-source file )
140140 },
141141 ...
142142 }
@@ -150,23 +150,33 @@ export default config
150150With this configuration: ` src/Component.ts ` → ` src/__generated__/GetUser.ts ` , matching Apollo
151151Tooling's output structure exactly.
152152
153- If you need to generate files per-component (default to GraphQL Codegen), remove the following
153+ If you need to generate files per-source file (default to GraphQL Codegen), remove the following
154154config option: ` filePerOperation: true ` (or set it to ` false ` ). Then, the output will be:
155155` src/Component.ts ` → ` src/__generated__/Component.ts `
156156
157- ## Type naming conventions
157+ ## Nested field types naming conventions
158158
159- Apollo Tooling generates type names using ** only field names** , omitting the GraphQL object type
160- name:
159+ Nested fields are the fields inside operation result object.
160+
161+ Apollo-tooling generates nested field type names using ** only field names** , omitting the GraphQL
162+ object type name:
161163
162164``` ts
163- // Apollo Tooling output
165+ // Apollo-tooling output
164166export type GetUserQuery_user = { ... };
165167export type GetUserQuery_user_address = { ... };
166168```
167169
168- To achieve similar naming with GraphQL Codegen use the ` extractAllFieldsToTypesCompact: true `
169- configuration option:
170+ By default GraphQL Codegen always adds field types:
171+
172+ ``` ts
173+ // GraphQL Codegen output
174+ export type GetUserQuery_user_User = { ... };
175+ export type GetUserQuery_user_User_address_Address = { ... };
176+ ```
177+
178+ To achieve naming similar to apollo-tooling with GraphQL Codegen use the
179+ ` extractAllFieldsToTypesCompact: true ` configuration option:
170180
171181``` ts filename="codegen.ts"
172182const config: CodegenConfig = {
@@ -183,11 +193,11 @@ const config: CodegenConfig = {
183193
184194## Enum types
185195
186- Apollo Tooling generates enums as native TypeScript ` enum ` declarations and references them directly
196+ Apollo-tooling generates enums as native TypeScript ` enum ` declarations and references them directly
187197by name (without any namespace prefix):
188198
189199``` ts
190- // Apollo Tooling output
200+ // Apollo-tooling output
191201export enum UserManagerRoleType {
192202 ROLE_TYPE_1 = ' ROLE_TYPE_1' ,
193203 ROLE_TYPE_2 = ' ROLE_TYPE_2' ,
@@ -200,7 +210,7 @@ export type GetUserQuery_user_manager = {
200210` ` `
201211
202212GraphQL Code Generator generates string literal union types by default. To produce native ` enum `
203- declarations matching Apollo Tooling 's output, set ` enumType : ' native' ` :
213+ declarations matching apollo-tooling 's output, set ` enumType : ' native' ` :
204214
205215` ` ` ts filename =" codegen.ts"
206216const config: CodegenConfig = {
@@ -233,7 +243,7 @@ for all available enum type options.
233243
234244## Recommended configuration
235245
236- Below is a configuration that produces output closely matching Apollo Tooling 's behaviour, including
246+ Below is a configuration that produces output closely matching apollo-tooling 's behaviour, including
237247per-file generation, enum output, and type naming:
238248
239249` ` ` ts filename =" codegen.ts"
@@ -243,7 +253,7 @@ const config: CodegenConfig = {
243253 // since codegen 6.0, we can also pass pre-parsed GraphQLSchema here (for caching - when running codegen in a loop on a monorepo with many packages)
244254 schema: ' ./schema.graphql' ,
245255 documents: [' ./src/**/*.{ts,tsx}' , ' !./src/**/__generated__/**' ],
246- // `externalDocuments` are read-only documents that are loaded but do not generate output .
256+ // `externalDocuments` are read-only documents that are loaded but no output is generated for them .
247257 // They are used to include fragments from another package in a monorepo without generating unnecessary files.
248258 externalDocuments: [
249259 ' ../other-package/src/**/*.{ts,tsx}' ,
@@ -262,13 +272,13 @@ const config: CodegenConfig = {
262272 }
263273 },
264274 config: {
265- // Extract nested field types to named types (matches Apollo Tooling naming)
275+ // Extract nested field types to named types (matches apollo-tooling naming)
266276 extractAllFieldsToTypesCompact: true ,
267277 // Keep original naming as-is (no camelCase conversion)
268278 namingConvention: ' keep' ,
269279 // Print each field on its own line for readability
270280 printFieldsOnNewLines: true ,
271- // Use native TypeScript enums (matches Apollo Tooling enum output)
281+ // Use native TypeScript enums (matches apollo-tooling enum output)
272282 enumType: ' native' ,
273283 // Always include __typename in result types
274284 nonOptionalTypename: true ,
@@ -278,7 +288,7 @@ const config: CodegenConfig = {
278288 omitOperationSuffix: true ,
279289 // Don't add 'Fragment' suffix to fragment result types
280290 fragmentSuffix: ' ' ,
281- // Default is 'unknown'; to match Apollo tooling we need to put 'any'
291+ // Default is 'unknown'; to match apollo- tooling we need to put 'any'
282292 defaultScalarType: ' any'
283293 }
284294}
@@ -288,17 +298,17 @@ export default config
288298
289299## Manual changes
290300
291- The setup above closely mimics Apollo Tooling but isn’t an exact match. The following items may
292- require manual fixes:
301+ The setup above closely mimics apollo-tooling (99.98% similarity in our tests) but isn’t an exact
302+ match. The following items may require manual fixes:
293303
294304#### 1. Nested field types naming.
295305
296- In very rare cases, the field names generated by GraphQL Codegen don’t match Apollo Tooling ’s.
306+ In very rare cases, the field names generated by GraphQL Codegen don’t match apollo-tooling ’s.
297307Update these cases manually.
298308
299309#### 2. Enum file location.
300310
301- Occasionally, GraphQL Codegen places enums in a different file then Apollo Tooling . If an enum is
311+ Occasionally, GraphQL Codegen places enums in a different file then apollo-tooling . If an enum is
302312missing, check nearby generated files and adjust your imports accordingly.
303313
304314#### 3. ` is possibly null ` and ` has any type ` typecheck bugs.
0 commit comments