forked from dotansimha/graphql-code-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase-documents-visitor.ts
More file actions
435 lines (404 loc) · 14.4 KB
/
base-documents-visitor.ts
File metadata and controls
435 lines (404 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
import autoBind from 'auto-bind';
import { pascalCase } from 'change-case-all';
import {
FragmentDefinitionNode,
GraphQLSchema,
OperationDefinitionNode,
OperationTypeNode,
VariableDefinitionNode,
} from 'graphql';
import { BaseVisitor, type RawConfig, type ParsedConfig } from './base-visitor.js';
import { DEFAULT_SCALARS } from './scalars.js';
import { SelectionSetToObject } from './selection-set-to-object.js';
import { NormalizedScalarsMap, CustomDirectivesConfig } from './types.js';
import { buildScalarsFromConfig, DeclarationBlock, DeclarationBlockConfig, getConfigValue } from './utils.js';
import { OperationVariablesToObject } from './variables-to-object.js';
export interface ParsedDocumentsConfig extends ParsedConfig {
extractAllFieldsToTypes: boolean;
extractAllFieldsToTypesCompact: boolean;
operationResultSuffix: string;
dedupeOperationSuffix: boolean;
omitOperationSuffix: boolean;
namespacedImportName: string | null;
exportFragmentSpreadSubTypes: boolean;
skipTypeNameForRoot: boolean;
nonOptionalTypename: boolean;
globalNamespace: boolean;
experimentalFragmentVariables: boolean;
mergeFragmentTypes: boolean;
customDirectives: CustomDirectivesConfig;
generatesOperationTypes: boolean;
importSchemaTypesFrom: string;
}
export interface RawDocumentsConfig extends RawConfig {
/**
* @default false
* @description Avoid adding `__typename` for root types. This is ignored when a selection explicitly specifies `__typename`.
*
* @exampleMarkdown
* ```ts filename="codegen.ts"
* import type { CodegenConfig } from '@graphql-codegen/cli';
*
* const config: CodegenConfig = {
* // ...
* generates: {
* 'path/to/file': {
* // plugins...
* config: {
* skipTypeNameForRoot: true
* },
* },
* },
* };
* export default config;
* ```
*/
skipTypeNameForRoot?: boolean;
/**
* @default false
* @description Automatically adds `__typename` field to the generated types, even when they are not specified
* in the selection set, and makes it non-optional
*
* @exampleMarkdown
* ```ts filename="codegen.ts"
* import type { CodegenConfig } from '@graphql-codegen/cli';
*
* const config: CodegenConfig = {
* // ...
* generates: {
* 'path/to/file': {
* // plugins...
* config: {
* nonOptionalTypename: true
* },
* },
* },
* };
* export default config;
* ```
*/
nonOptionalTypename?: boolean;
/**
* @default false
* @description Puts all generated code under `global` namespace. Useful for Stencil integration.
*
* @exampleMarkdown
* ```ts filename="codegen.ts"
* import type { CodegenConfig } from '@graphql-codegen/cli';
*
* const config: CodegenConfig = {
* // ...
* generates: {
* 'path/to/file': {
* // plugins...
* config: {
* globalNamespace: true
* },
* },
* },
* };
* export default config;
* ```
*/
globalNamespace?: boolean;
/**
* @default ""
* @description Adds a suffix to generated operation result type names
*/
operationResultSuffix?: string;
/**
* @default false
* @description Set this configuration to `true` if you wish to make sure to remove duplicate operation name suffix.
*/
dedupeOperationSuffix?: boolean;
/**
* @default false
* @description Set this configuration to `true` if you wish to disable auto add suffix of operation name, like `Query`, `Mutation`, `Subscription`, `Fragment`.
*/
omitOperationSuffix?: boolean;
/**
* @default false
* @description If set to true, it will export the sub-types created in order to make it easier to access fields declared under fragment spread.
*/
exportFragmentSpreadSubTypes?: boolean;
/**
* @default false
* @description If set to true, it will enable support for parsing variables on fragments.
*/
experimentalFragmentVariables?: boolean;
/**
* @default false
* @description If set to true, merge equal fragment interfaces.
*/
mergeFragmentTypes?: boolean;
// The following are internal, and used by presets
/**
* @ignore
*/
namespacedImportName?: string;
/**
* @description Configures behavior for use with custom directives from
* various GraphQL libraries.
* @exampleMarkdown
* ```ts filename="codegen.ts"
* import type { CodegenConfig } from '@graphql-codegen/cli';
*
* const config: CodegenConfig = {
* // ...
* generates: {
* 'path/to/file.ts': {
* plugins: ['typescript'],
* config: {
* customDirectives: {
* apolloUnmask: true
* }
* },
* },
* },
* };
* export default config;
* ```
*/
customDirectives?: CustomDirectivesConfig;
/**
* @description Whether to generate operation types such as Variables, Query/Mutation/Subscription selection set, and Fragment types
* This can be used with `importSchemaTypesFrom` to generate shared used Enums and Input.
* @default true
* @exampleMarkdown
* ```ts filename="codegen.ts"
* import type { CodegenConfig } from '@graphql-codegen/cli';
*
* const config: CodegenConfig = {
* // ...
* generates: {
* 'path/to/file.ts': {
* plugins: ['typescript-operations'],
* config: {
* generatesOperationTypes: false,
* },
* },
* },
* };
* export default config;
* ```
*/
generatesOperationTypes?: boolean;
/**
* @description The absolute (prefixed with `~`) or relative path from `cwd` to the shared used Enums and Input (See `generatesOperationTypes`).
* @default true
* @exampleMarkdown
* ```ts filename="codegen.ts"
* import type { CodegenConfig } from '@graphql-codegen/cli';
*
* const config: CodegenConfig = {
* // ...
* generates: {
* 'path/to/file.ts': {
* plugins: ['typescript-operations'],
* config: {
* importSchemaTypesFrom: './path/to/shared-types.ts', // relative
* importSchemaTypesFrom: '~@my-org/package' // absolute
* },
* },
* },
* };
* export default config;
* ```
*/
importSchemaTypesFrom?: string;
/**
* @default false
* @description Extract all field types to their own types, instead of inlining them.
* This helps to reduce type duplication, and makes type errors more readable.
* It can also significantly reduce the size of the generated code, the generation time,
* and the typechecking time.
*/
extractAllFieldsToTypes?: boolean;
/**
* @default false
* @description Generates type names using only field names, omitting GraphQL type names.
* This matches the naming convention used by Apollo Tooling.
* For example, instead of `Query_company_Company_office_Office_location_Location`,
* it generates `Query_company_office_location`.
*
* When this option is enabled, `extractAllFieldsToTypes` is automatically enabled as well.
*/
extractAllFieldsToTypesCompact?: boolean;
}
export class BaseDocumentsVisitor<
TRawConfig extends RawDocumentsConfig = RawDocumentsConfig,
TPluginConfig extends ParsedDocumentsConfig = ParsedDocumentsConfig
> extends BaseVisitor<TRawConfig, TPluginConfig> {
protected _unnamedCounter = 1;
protected _variablesTransfomer: OperationVariablesToObject;
protected _selectionSetToObject: SelectionSetToObject;
protected _globalDeclarations: Set<string> = new Set<string>();
constructor(
rawConfig: TRawConfig,
additionalConfig: TPluginConfig,
protected _schema: GraphQLSchema,
defaultScalars: NormalizedScalarsMap = DEFAULT_SCALARS
) {
super(rawConfig, {
exportFragmentSpreadSubTypes: getConfigValue(rawConfig.exportFragmentSpreadSubTypes, false),
dedupeOperationSuffix: getConfigValue(rawConfig.dedupeOperationSuffix, false),
omitOperationSuffix: getConfigValue(rawConfig.omitOperationSuffix, false),
skipTypeNameForRoot: getConfigValue(rawConfig.skipTypeNameForRoot, false),
nonOptionalTypename: getConfigValue(rawConfig.nonOptionalTypename, false),
namespacedImportName: getConfigValue(rawConfig.namespacedImportName, null),
experimentalFragmentVariables: getConfigValue(rawConfig.experimentalFragmentVariables, false),
globalNamespace: !!rawConfig.globalNamespace,
operationResultSuffix: getConfigValue(rawConfig.operationResultSuffix, ''),
scalars: buildScalarsFromConfig(_schema, rawConfig, defaultScalars),
customDirectives: getConfigValue(rawConfig.customDirectives, { apolloUnmask: false }),
generatesOperationTypes: getConfigValue(rawConfig.generatesOperationTypes, true),
importSchemaTypesFrom: getConfigValue(rawConfig.importSchemaTypesFrom, ''),
extractAllFieldsToTypes:
getConfigValue(rawConfig.extractAllFieldsToTypes, false) ||
getConfigValue(rawConfig.extractAllFieldsToTypesCompact, false),
extractAllFieldsToTypesCompact: getConfigValue(rawConfig.extractAllFieldsToTypesCompact, false),
...((additionalConfig || {}) as any),
});
autoBind(this);
this._variablesTransfomer = new OperationVariablesToObject(
this.scalars,
this.convertName,
this.config.namespacedImportName
);
}
public getGlobalDeclarations(noExport = false): string[] {
return Array.from(this._globalDeclarations).map(t => (noExport ? t : `export ${t}`));
}
setSelectionSetHandler(handler: SelectionSetToObject): void {
this._selectionSetToObject = handler;
}
setDeclarationBlockConfig(config: DeclarationBlockConfig): void {
this._declarationBlockConfig = config;
}
setVariablesTransformer(variablesTransfomer: OperationVariablesToObject): void {
this._variablesTransfomer = variablesTransfomer;
}
public get schema(): GraphQLSchema {
return this._schema;
}
private handleAnonymousOperation(node: OperationDefinitionNode): string {
const name = node.name?.value;
if (name) {
return this.convertName(name, {
useTypesPrefix: false,
useTypesSuffix: false,
});
}
return this.convertName(String(this._unnamedCounter++), {
prefix: 'Unnamed_',
suffix: '_',
useTypesPrefix: false,
useTypesSuffix: false,
});
}
FragmentDefinition(node: FragmentDefinitionNode): string {
if (!this.config.generatesOperationTypes) {
return null;
}
const fragmentRootType = this._schema.getType(node.typeCondition.name.value);
const selectionSet = this._selectionSetToObject.createNext(fragmentRootType, node.selectionSet);
const fragmentSuffix = this.getFragmentSuffix(node);
return [
selectionSet.transformFragmentSelectionSetToTypes(node.name.value, fragmentSuffix, this._declarationBlockConfig),
this.config.experimentalFragmentVariables
? new DeclarationBlock({
...this._declarationBlockConfig,
blockTransformer: t => this.applyVariablesWrapper(t),
})
.export()
.asKind('type')
.withName(
this.convertName(node.name.value, {
suffix: fragmentSuffix + 'Variables',
})
)
.withBlock(this._variablesTransfomer.transform(node.variableDefinitions)).string
: undefined,
]
.filter(r => r)
.join('\n\n');
}
protected applyVariablesWrapper(variablesBlock: string, _operationType?: string): string {
return variablesBlock;
}
OperationDefinition(node: OperationDefinitionNode): string | null {
if (!this.config.generatesOperationTypes) {
return null;
}
const name = this.handleAnonymousOperation(node);
const operationRootType = getRootType(node.operation, this._schema);
if (!operationRootType) {
throw new Error(`Unable to find root schema type for operation type "${node.operation}"!`);
}
const selectionSet = this._selectionSetToObject.createNext(operationRootType, node.selectionSet);
const visitedOperationVariables = this._variablesTransfomer.transform<VariableDefinitionNode>(
node.variableDefinitions
);
const operationType: string = pascalCase(node.operation);
const operationTypeSuffix = this.getOperationSuffix(name, operationType);
const selectionSetObjects = selectionSet.transformSelectionSet(
this.convertName(name, {
suffix: operationTypeSuffix,
})
);
const operationResultName = this.convertName(name, {
suffix: operationTypeSuffix + this._parsedConfig.operationResultSuffix,
});
// When extractAllFieldsToTypes creates a root type with the same name as the operation result,
// we only need the extracted type and can skip the alias to avoid duplicates
const shouldSkipOperationResult =
this._parsedConfig.extractAllFieldsToTypesCompact && operationResultName === selectionSetObjects.mergedTypeString;
const operationResult = shouldSkipOperationResult
? ''
: new DeclarationBlock(this._declarationBlockConfig)
.export()
.asKind('type')
.withName(operationResultName)
.withContent(selectionSetObjects.mergedTypeString).string;
const operationVariables = new DeclarationBlock({
...this._declarationBlockConfig,
blockTransformer: t => this.applyVariablesWrapper(t, operationType),
})
.export()
.asKind('type')
.withName(
this.convertName(name, {
suffix: operationTypeSuffix + 'Variables',
})
)
.withBlock(visitedOperationVariables).string;
const dependentTypesContent = this._parsedConfig.extractAllFieldsToTypes
? selectionSetObjects.dependentTypes.map(
i =>
new DeclarationBlock(this._declarationBlockConfig)
.export()
.asKind('type')
.withName(i.name)
.withContent(i.content).string
)
: [];
return [
...(dependentTypesContent.length > 0 ? [dependentTypesContent.join('\n')] : []),
operationVariables,
operationResult,
]
.filter(r => r)
.join('\n\n');
}
}
function getRootType(operation: OperationTypeNode, schema: GraphQLSchema) {
switch (operation) {
case 'query':
return schema.getQueryType();
case 'mutation':
return schema.getMutationType();
case 'subscription':
return schema.getSubscriptionType();
}
throw new Error(`Unknown operation type: ${operation}`);
}