Skip to content

Commit f5b38f9

Browse files
authored
Update migration guide with near-operation file (#10630)
1 parent c61ab17 commit f5b38f9

1 file changed

Lines changed: 65 additions & 4 deletions

File tree

website/src/pages/docs/migration/operations-and-client-preset-from-5-0.mdx

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,23 +92,23 @@ Some repos may have multiple Codegen projects, each generating types for operati
9292
const config: CodegenConfig = {
9393
// ...
9494
generates: {
95-
'src/shared/base-types.generated.ts': {
95+
'src/graphql/base-types.generated.ts': {
9696
plugins: ['typescript']
9797
},
9898
'src/project-1/types.generated.ts': {
9999
documents: 'src/project-1/**/*.graphql.ts',
100100
preset: 'import-types',
101101
plugins: ['typescript-operations'],
102102
presetConfig: {
103-
typesPath: '../shared/base-types.generated.ts'
103+
typesPath: '../graphql/base-types.generated.ts'
104104
}
105105
},
106106
'src/project-2/types.generated.ts': {
107107
documents: 'src/project-2/**/*.graphql.ts',
108108
preset: 'import-types',
109109
plugins: ['typescript-operations'],
110110
presetConfig: {
111-
typesPath: '../shared/base-types.generated.ts'
111+
typesPath: '../graphql/base-types.generated.ts'
112112
}
113113
}
114114
}
@@ -121,7 +121,7 @@ Now, it is possible to do this with just `typescript-operations`, as it supports
121121
const config: CodegenConfig = {
122122
// ...
123123
generates: {
124-
'src/shared/base-types.generated.ts': {
124+
'src/graphql/base-types.generated.ts': {
125125
documents: 'src/**/*.graphql.ts' // Parses all files with GraphQL documents to generate Enum and Input types that are used by every project
126126
plugins: ['typescript-operations'],
127127
config: {
@@ -146,6 +146,67 @@ const config: CodegenConfig = {
146146
}
147147
```
148148

149+
#### near-operation-file-preset setups
150+
151+
[near-operation-file-preset](https://the-guild.dev/graphql/codegen/plugins/presets/near-operation-file-preset) is commonly used to generate a file next to operation document a.k.a. near-operation file.
152+
153+
Previously, shared types were always generated into a shared file using the `typescript` plugin, and reused in every near-operation file:
154+
155+
```typescript filename="codegen.ts"
156+
const config: CodegenConfig = {
157+
// ...
158+
generates: {
159+
'src/graphql/base-types.generated.ts': {
160+
plugins: ['typescript']
161+
},
162+
'src/': {
163+
preset: 'near-operation-file',
164+
presetConfig: {
165+
baseTypesPath: './graphql/base-types.generated.ts'
166+
},
167+
plugins: ['typescript-operations']
168+
}
169+
}
170+
}
171+
```
172+
173+
Now, it is still possible to reuse shared types, but with the decoupling from the `typescript` plugin, we can just use `typescript-operations` for both base type and operation type generation.
174+
175+
```typescript filename="codegen.ts"
176+
const config: CodegenConfig = {
177+
// ...
178+
generates: {
179+
'src/graphql/base-types.generated.ts': {
180+
plugins: ['typescript-operations'],
181+
config: {
182+
generateOperationTypes: false
183+
}
184+
},
185+
'src/': {
186+
preset: 'near-operation-file',
187+
plugins: ['typescript-operations'],
188+
config: {
189+
importSchemaTypesFrom: 'src/graphql/base-types.generated.ts'
190+
}
191+
}
192+
}
193+
}
194+
```
195+
196+
Alternatively, base types and operation types can all be generated into every near-operation file:
197+
198+
```typescript filename="codegen.ts"
199+
const config: CodegenConfig = {
200+
// ...
201+
generates: {
202+
'src/': {
203+
preset: 'near-operation-file',
204+
plugins: ['typescript-operations']
205+
}
206+
}
207+
}
208+
```
209+
149210
## Breaking changes
150211

151212
1. Object types are no longer generated

0 commit comments

Comments
 (0)