Skip to content

Commit 770fe0d

Browse files
ikusakov2ikusakoveddeee888
authored
More documentation for migrating from Apollo Tooling to GraphQL Codegen (#10638)
* apollo-tooling migration docs updated * prettier * fix prettier bug --------- Co-authored-by: Igor Kusakov <[email protected]> Co-authored-by: Eddy Nguyen <[email protected]>
1 parent 8c4db2a commit 770fe0d

1 file changed

Lines changed: 78 additions & 20 deletions

File tree

website/src/pages/docs/migration/apollo-tooling.mdx

Lines changed: 78 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -77,24 +77,37 @@ apollo client:codegen --target=typescript --outputFlat src/__generated__/types.t
7777
</Tabs.Tab>
7878

7979
<Tabs.Tab>
80+
8081
```ts filename="codegen.ts"
8182
import type { CodegenConfig } from '@graphql-codegen/cli'
8283

8384
const config: CodegenConfig = {
84-
schema: './schema.graphql',
85-
documents: ['./src/**/*.{ts,tsx}', '!./src/**/__generated__/**'],
86-
generates: {
87-
'./src/': {
88-
preset: 'near-operation-file',
89-
presetConfig: {
90-
extension: '.ts',
91-
folder: '**generated**',
92-
filePerOperation: true,
93-
inGeneratesOnly: true,
94-
},
95-
plugins: ['typescript-operations'],
96-
},
97-
},
85+
schema: './schema.graphql',
86+
documents: ['./src/**/*.{ts,tsx}', '!./src/**/__generated__/**'],
87+
generates: {
88+
'./src/': {
89+
preset: 'near-operation-file',
90+
presetConfig: {
91+
extension: '.ts',
92+
folder: '__generated__',
93+
filePerOperation: true,
94+
inGeneratesOnly: true
95+
},
96+
plugins: ['typescript-operations'],
97+
config: {
98+
namingConvention: 'keep',
99+
extractAllFieldsToTypesCompact: true,
100+
printFieldsOnNewLines: true,
101+
enumType: 'native',
102+
nonOptionalTypename: true,
103+
skipTypeNameForRoot: true,
104+
omitOperationSuffix: true,
105+
fragmentSuffix: '',
106+
generatesOperationTypes: true,
107+
defaultScalarType: 'any'
108+
}
109+
}
110+
}
98111
}
99112

100113
export default config
@@ -147,7 +160,11 @@ const config: CodegenConfig = {
147160
export default config
148161
```
149162

150-
With this configuration, `src/Component.ts``src/__generated__/Component.ts`, matching Apollo Tooling's output structure exactly.
163+
With this configuration, `src/Component.ts``src/__generated__/GetUser.ts`, matching Apollo Tooling's output structure exactly.
164+
165+
If you need to generate files per-component (default to GraphQL Codegen), remove the following
166+
config option: `filePerOperation: true` (or set it to `false`). Then, the output will be:
167+
`src/Component.ts``src/__generated__/Component.ts`
151168

152169
## Type naming conventions
153170

@@ -218,6 +235,7 @@ const config: CodegenConfig = {
218235
plugins: ['typescript-operations'],
219236
config: {
220237
extractAllFieldsToTypesCompact: true,
238+
namingConvention: 'keep',
221239
enumType: 'native'
222240
}
223241
}
@@ -275,7 +293,10 @@ const config: CodegenConfig = {
275293
// Don't add 'Query'/'Mutation'/'Subscription' suffixes to operation result types
276294
omitOperationSuffix: true,
277295
// Don't add 'Fragment' suffix to fragment result types
278-
fragmentSuffix: ''
296+
fragmentSuffix: '',
297+
generatesOperationTypes: true,
298+
// Default is 'unknown'; to match Apollo tooling we need to put 'any'
299+
defaultScalarType: 'any'
279300
}
280301
}
281302
}
@@ -288,13 +309,50 @@ export default config
288309

289310
The setup above closely mimics Apollo Tooling but isn’t an exact match. The following items may require manual fixes:
290311

291-
1. Nested field types naming. In very rare cases, the names generated by GraphQL Codegen don’t match Apollo Tooling’s. Update these cases manually.
312+
1. Nested field types naming.
313+
314+
In very rare cases, the names generated by GraphQL Codegen don’t match Apollo Tooling’s. Update these cases manually.
315+
316+
2. Enum file location.
292317

293-
2. Enum file location. Occasionally, GraphQL Codegen places enums in a different file. If an enum is missing, check nearby generated files and adjust your imports accordingly.
318+
Occasionally, GraphQL Codegen places enums in a different file then Apollo Tooling. If an enum is missing, check nearby generated files and adjust your imports accordingly.
294319

295-
3. Occasional mismatch between `Type | null` and `Type | null | undefined`.
320+
3. `is possibly null` and `has any type` typecheck bugs.
321+
322+
These bugs has to be fixed.
323+
324+
For `is possibly null` bug, asserting for not null or adding `!` will fix most cases:
325+
326+
```
327+
getUser.name -> getUser!.name
328+
```
296329

297-
4. Occasional `is possibly null` and `has any type` typecheck bugs.
330+
For `has any type` bug - a proper type needs to be determined.
331+
332+
4. Mismatch between `Type | null` and `Type | null | undefined`.
333+
334+
Experiment with the following configuration options to keep your codebase changes to a minimum:
335+
336+
```
337+
maybeValue: defaults to 'T | null', set to 'T | null | undefined' if necessary
338+
inputMaybeValue: defaults to 'Maybe<T>', set to 'T | null | undefined' if necessary
339+
avoidOptionals: Replaces ? optional modifier with explicit Maybe<T>. Supports granular control via object form.
340+
allowUndefinedQueryVariables: Adds | undefined to Query operation variable types (not Mutation/Subscription)
341+
optionalResolveType: Makes __resolveType optional (__resolveType?) in resolver types.
342+
nullability: When errorHandlingClient: true, adjusts nullability for fields marked with @semanticNonNull directive (requires graphql-sock).
343+
```
344+
345+
5. Extra `__typename` present, or required `__typename` missing.
346+
347+
Experiment with the following configuration options to keep your codebase changes to a minimum:
348+
349+
```
350+
skipTypename: prevents adding __typename to generated types unless explicitly in the selection set.
351+
skipTypeNameForRoot: skips __typename specifically for root types (Query, Mutation, Subscription). Ignored if __typename is explicitly in the selection set
352+
nonOptionalTypename: always adds __typename and makes it a required (non-optional) field.
353+
addTypenameToSelectionSets: injects __typename directly into the generated document node selection sets.
354+
resolversNonOptionalTypename: makes __typename non-optional in resolver mappings without affecting base types. Supports granular control via object form.
355+
```
298356

299357
## Further reading
300358

0 commit comments

Comments
 (0)