UNPKG

18.1 kBTypeScriptView Raw
1import { DirectiveDefinitionNode, DirectiveNode, EnumTypeDefinitionNode, EnumValueDefinitionNode, FieldDefinitionNode, GraphQLSchema, InputObjectTypeDefinitionNode, InputValueDefinitionNode, InterfaceTypeDefinitionNode, ListTypeNode, NamedTypeNode, NameNode, NonNullTypeNode, ObjectTypeDefinitionNode, ScalarTypeDefinitionNode, StringValueNode, UnionTypeDefinitionNode } from 'graphql';
2import { BaseVisitor, ParsedConfig, RawConfig } from './base-visitor.js';
3import { DeclarationKind, DeclarationKindConfig, DirectiveArgumentAndInputFieldMappings, EnumValuesMap, NormalizedScalarsMap, ParsedDirectiveArgumentAndInputFieldMappings, ParsedEnumValuesMap } from './types.js';
4import { DeclarationBlock, DeclarationBlockConfig } from './utils.js';
5import { OperationVariablesToObject } from './variables-to-object.js';
6export interface ParsedTypesConfig extends ParsedConfig {
7 enumValues: ParsedEnumValuesMap;
8 declarationKind: DeclarationKindConfig;
9 addUnderscoreToArgsType: boolean;
10 onlyEnums: boolean;
11 onlyOperationTypes: boolean;
12 enumPrefix: boolean;
13 enumSuffix: boolean;
14 fieldWrapperValue: string;
15 wrapFieldDefinitions: boolean;
16 entireFieldWrapperValue: string;
17 wrapEntireDefinitions: boolean;
18 ignoreEnumValuesFromSchema: boolean;
19 directiveArgumentAndInputFieldMappings: ParsedDirectiveArgumentAndInputFieldMappings;
20}
21export interface RawTypesConfig extends RawConfig {
22 /**
23 * @description Adds `_` to generated `Args` types in order to avoid duplicate identifiers.
24 *
25 * @exampleMarkdown
26 * ## With Custom Values
27 *
28 * ```ts filename="codegen.ts"
29 * import type { CodegenConfig } from '@graphql-codegen/cli';
30 *
31 * const config: CodegenConfig = {
32 * // ...
33 * generates: {
34 * 'path/to/file': {
35 * // plugins...
36 * config: {
37 * addUnderscoreToArgsType: true
38 * },
39 * },
40 * },
41 * };
42 * export default config;
43 * ```
44 */
45 addUnderscoreToArgsType?: boolean;
46 /**
47 * @description Overrides the default value of enum values declared in your GraphQL schema.
48 * You can also map the entire enum to an external type by providing a string that of `module#type`.
49 *
50 * @exampleMarkdown
51 * ## With Custom Values
52 * ```ts filename="codegen.ts"
53 * import type { CodegenConfig } from '@graphql-codegen/cli';
54 *
55 * const config: CodegenConfig = {
56 * // ...
57 * generates: {
58 * 'path/to/file': {
59 * // plugins...
60 * config: {
61 * enumValues: {
62 * MyEnum: {
63 * A: 'foo'
64 * }
65 * }
66 * },
67 * },
68 * },
69 * };
70 * export default config;
71 * ```
72 *
73 * ## With External Enum
74 * ```ts filename="codegen.ts"
75 * import type { CodegenConfig } from '@graphql-codegen/cli';
76 *
77 * const config: CodegenConfig = {
78 * // ...
79 * generates: {
80 * 'path/to/file': {
81 * // plugins...
82 * config: {
83 * enumValues: {
84 * MyEnum: './my-file#MyCustomEnum',
85 * }
86 * },
87 * },
88 * },
89 * };
90 * export default config;
91 * ```
92 *
93 * ## Import All Enums from a file
94 * ```ts filename="codegen.ts"
95 * import type { CodegenConfig } from '@graphql-codegen/cli';
96 *
97 * const config: CodegenConfig = {
98 * // ...
99 * generates: {
100 * 'path/to/file': {
101 * // plugins...
102 * config: {
103 * enumValues: {
104 * MyEnum: './my-file',
105 * }
106 * },
107 * },
108 * },
109 * };
110 * export default config;
111 * ```
112 */
113 enumValues?: EnumValuesMap;
114 /**
115 * @description Overrides the default output for various GraphQL elements.
116 *
117 * @exampleMarkdown
118 * ## Override all declarations
119 *
120 * ```ts filename="codegen.ts"
121 * import type { CodegenConfig } from '@graphql-codegen/cli';
122 *
123 * const config: CodegenConfig = {
124 * // ...
125 * generates: {
126 * 'path/to/file': {
127 * // plugins...
128 * config: {
129 * declarationKind: 'interface'
130 * },
131 * },
132 * },
133 * };
134 * export default config;
135 * ```
136 *
137 * ## Override only specific declarations
138 *
139 * ```ts filename="codegen.ts"
140 * import type { CodegenConfig } from '@graphql-codegen/cli';
141 *
142 * const config: CodegenConfig = {
143 * // ...
144 * generates: {
145 * 'path/to/file': {
146 * // plugins...
147 * config: {
148 * declarationKind: {
149 * type: 'interface',
150 * input: 'interface'
151 * }
152 * },
153 * },
154 * },
155 * };
156 * export default config;
157 * ```
158 */
159 declarationKind?: DeclarationKind | DeclarationKindConfig;
160 /**
161 * @default true
162 * @description Allow you to disable prefixing for generated enums, works in combination with `typesPrefix`.
163 *
164 * @exampleMarkdown
165 * ## Disable enum prefixes
166 *
167 * ```ts filename="codegen.ts"
168 * import type { CodegenConfig } from '@graphql-codegen/cli';
169 *
170 * const config: CodegenConfig = {
171 * // ...
172 * generates: {
173 * 'path/to/file': {
174 * // plugins...
175 * config: {
176 * typesPrefix: 'I',
177 * enumPrefix: false
178 * },
179 * },
180 * },
181 * };
182 * export default config;
183 * ```
184 */
185 enumPrefix?: boolean;
186 /**
187 * @default true
188 * @description Allow you to disable suffixing for generated enums, works in combination with `typesSuffix`.
189 *
190 * @exampleMarkdown
191 * ## Disable enum suffixes
192 *
193 * ```ts filename="codegen.ts"
194 * import type { CodegenConfig } from '@graphql-codegen/cli';
195 *
196 * const config: CodegenConfig = {
197 * // ...
198 * generates: {
199 * 'path/to/file': {
200 * // plugins...
201 * config: {
202 * typesSuffix: 'I',
203 * enumSuffix: false
204 * },
205 * },
206 * },
207 * };
208 * export default config;
209 * ```
210 */
211 enumSuffix?: boolean;
212 /**
213 * @description Allow you to add wrapper for field type, use T as the generic value. Make sure to set `wrapFieldDefinitions` to `true` in order to make this flag work.
214 * @default T
215 *
216 * @exampleMarkdown
217 * ## Allow Promise
218 *
219 * ```ts filename="codegen.ts"
220 * import type { CodegenConfig } from '@graphql-codegen/cli';
221 *
222 * const config: CodegenConfig = {
223 * // ...
224 * generates: {
225 * 'path/to/file': {
226 * // plugins...
227 * config: {
228 * wrapFieldDefinitions: true,
229 * fieldWrapperValue: 'T | Promise<T>',
230 * },
231 * },
232 * },
233 * };
234 * export default config;
235 * ```
236 */
237 fieldWrapperValue?: string;
238 /**
239 * @description Set to `true` in order to wrap field definitions with `FieldWrapper`.
240 * This is useful to allow return types such as Promises and functions.
241 * @default false
242 *
243 * @exampleMarkdown
244 * ## Enable wrapping fields
245 *
246 * ```ts filename="codegen.ts"
247 * import type { CodegenConfig } from '@graphql-codegen/cli';
248 *
249 * const config: CodegenConfig = {
250 * // ...
251 * generates: {
252 * 'path/to/file': {
253 * // plugins...
254 * config: {
255 * wrapFieldDefinitions: true,
256 * },
257 * },
258 * },
259 * };
260 * export default config;
261 * ```
262 */
263 wrapFieldDefinitions?: boolean;
264 /**
265 * @description This will cause the generator to emit types for enums only
266 * @default false
267 *
268 * @exampleMarkdown
269 * ## Override all definition types
270 *
271 * ```ts filename="codegen.ts"
272 * import type { CodegenConfig } from '@graphql-codegen/cli';
273 *
274 * const config: CodegenConfig = {
275 * // ...
276 * generates: {
277 * 'path/to/file': {
278 * // plugins...
279 * config: {
280 * onlyEnums: true,
281 * },
282 * },
283 * },
284 * };
285 * export default config;
286 * ```
287 */
288 onlyEnums?: boolean;
289 /**
290 * @description This will cause the generator to emit types for operations only (basically only enums and scalars)
291 * @default false
292 *
293 * @exampleMarkdown
294 * ## Override all definition types
295 *
296 * ```ts filename="codegen.ts"
297 * import type { CodegenConfig } from '@graphql-codegen/cli';
298 *
299 * const config: CodegenConfig = {
300 * // ...
301 * generates: {
302 * 'path/to/file': {
303 * // plugins...
304 * config: {
305 * onlyOperationTypes: true,
306 * },
307 * },
308 * },
309 * };
310 * export default config;
311 * ```
312 */
313 onlyOperationTypes?: boolean;
314 /**
315 * @description This will cause the generator to ignore enum values defined in GraphQLSchema
316 * @default false
317 *
318 * @exampleMarkdown
319 * ## Ignore enum values from schema
320 *
321 * ```ts filename="codegen.ts"
322 * import type { CodegenConfig } from '@graphql-codegen/cli';
323 *
324 * const config: CodegenConfig = {
325 * // ...
326 * generates: {
327 * 'path/to/file': {
328 * // plugins...
329 * config: {
330 * ignoreEnumValuesFromSchema: true,
331 * },
332 * },
333 * },
334 * };
335 * export default config;
336 * ```
337 */
338 ignoreEnumValuesFromSchema?: boolean;
339 /**
340 * @name wrapEntireFieldDefinitions
341 * @type boolean
342 * @description Set to `true` in order to wrap field definitions with `EntireFieldWrapper`.
343 * This is useful to allow return types such as Promises and functions for fields.
344 * Differs from `wrapFieldDefinitions` in that this wraps the entire field definition if i.e. the field is an Array, while
345 * `wrapFieldDefinitions` will wrap every single value inside the array.
346 * @default true
347 *
348 * @example Enable wrapping entire fields
349 * ```ts filename="codegen.ts"
350 * import type { CodegenConfig } from '@graphql-codegen/cli';
351 *
352 * const config: CodegenConfig = {
353 * // ...
354 * generates: {
355 * 'path/to/file': {
356 * // plugins...
357 * config: {
358 * wrapEntireFieldDefinitions: false,
359 * },
360 * },
361 * },
362 * };
363 * export default config;
364 * ```
365 */
366 wrapEntireFieldDefinitions?: boolean;
367 /**
368 * @name entireFieldWrapperValue
369 * @type string
370 * @description Allow to override the type value of `EntireFieldWrapper`. This wrapper applies outside of Array and Maybe
371 * unlike `fieldWrapperValue`, that will wrap the inner type.
372 * @default T | Promise<T> | (() => T | Promise<T>)
373 *
374 * @example Only allow values
375 * ```ts filename="codegen.ts"
376 * import type { CodegenConfig } from '@graphql-codegen/cli';
377 *
378 * const config: CodegenConfig = {
379 * // ...
380 * generates: {
381 * 'path/to/file': {
382 * // plugins...
383 * config: {
384 * entireFieldWrapperValue: 'T',
385 * },
386 * },
387 * },
388 * };
389 * export default config;
390 * ```
391 */
392 entireFieldWrapperValue?: string;
393 /**
394 * @description Replaces a GraphQL scalar with a custom type based on the applied directive on an argument or input field.
395 *
396 * You can use both `module#type` and `module#namespace#type` syntax.
397 * Will NOT work with introspected schemas since directives are not exported.
398 * Only works with directives on ARGUMENT_DEFINITION or INPUT_FIELD_DEFINITION.
399 *
400 * **WARNING:** Using this option does only change the type definitions.
401 *
402 * For actually ensuring that a type is correct at runtime you will have to use schema transforms (e.g. with [@graphql-tools/utils mapSchema](https://graphql-tools.com/docs/schema-directives)) that apply those rules!
403 * Otherwise, you might end up with a runtime type mismatch which could cause unnoticed bugs or runtime errors.
404 *
405 * Please use this configuration option with care!
406 *
407 * @exampleMarkdown
408 * ## Custom Context Type\
409 * ```ts filename="codegen.ts"
410 * import type { CodegenConfig } from '@graphql-codegen/cli';
411 *
412 * const config: CodegenConfig = {
413 * // ...
414 * generates: {
415 * 'path/to/file': {
416 * // plugins...
417 * config: {
418 * directiveArgumentAndInputFieldMappings: {
419 * AsNumber: 'number',
420 * AsComplex: './my-models#Complex',
421 * }
422 * },
423 * },
424 * },
425 * };
426 * export default config;
427 * ```
428 */
429 directiveArgumentAndInputFieldMappings?: DirectiveArgumentAndInputFieldMappings;
430 /**
431 * @description Adds a suffix to the imported names to prevent name clashes.
432 *
433 * @exampleMarkdown
434 * ```ts filename="codegen.ts"
435 * import type { CodegenConfig } from '@graphql-codegen/cli';
436 *
437 * const config: CodegenConfig = {
438 * // ...
439 * generates: {
440 * 'path/to/file': {
441 * // plugins...
442 * config: {
443 * directiveArgumentAndInputFieldMappings: 'Model'
444 * },
445 * },
446 * },
447 * };
448 * export default config;
449 * ```
450 */
451 directiveArgumentAndInputFieldMappingTypeSuffix?: string;
452}
453export declare class BaseTypesVisitor<TRawConfig extends RawTypesConfig = RawTypesConfig, TPluginConfig extends ParsedTypesConfig = ParsedTypesConfig> extends BaseVisitor<TRawConfig, TPluginConfig> {
454 protected _schema: GraphQLSchema;
455 protected _argumentsTransformer: OperationVariablesToObject;
456 constructor(_schema: GraphQLSchema, rawConfig: TRawConfig, additionalConfig: TPluginConfig, defaultScalars?: NormalizedScalarsMap);
457 protected getExportPrefix(): string;
458 getFieldWrapperValue(): string;
459 getEntireFieldWrapperValue(): string;
460 getScalarsImports(): string[];
461 getDirectiveArgumentAndInputFieldMappingsImports(): string[];
462 get scalarsDefinition(): string;
463 get directiveArgumentAndInputFieldMappingsDefinition(): string;
464 setDeclarationBlockConfig(config: DeclarationBlockConfig): void;
465 setArgumentsTransformer(argumentsTransfomer: OperationVariablesToObject): void;
466 NonNullType(node: NonNullTypeNode): string;
467 getInputObjectDeclarationBlock(node: InputObjectTypeDefinitionNode): DeclarationBlock;
468 getInputObjectOneOfDeclarationBlock(node: InputObjectTypeDefinitionNode): DeclarationBlock;
469 InputObjectTypeDefinition(node: InputObjectTypeDefinitionNode): string;
470 InputValueDefinition(node: InputValueDefinitionNode): string;
471 Name(node: NameNode): string;
472 FieldDefinition(node: FieldDefinitionNode): string;
473 UnionTypeDefinition(node: UnionTypeDefinitionNode, key: string | number | undefined, parent: any): string;
474 protected mergeInterfaces(interfaces: string[], hasOtherFields: boolean): string;
475 appendInterfacesAndFieldsToBlock(block: DeclarationBlock, interfaces: string[], fields: string[]): void;
476 getObjectTypeDeclarationBlock(node: ObjectTypeDefinitionNode, originalNode: ObjectTypeDefinitionNode): DeclarationBlock;
477 protected mergeAllFields(allFields: string[], _hasInterfaces: boolean): string;
478 ObjectTypeDefinition(node: ObjectTypeDefinitionNode, key: number | string, parent: any): string;
479 getInterfaceTypeDeclarationBlock(node: InterfaceTypeDefinitionNode, _originalNode: InterfaceTypeDefinitionNode): DeclarationBlock;
480 InterfaceTypeDefinition(node: InterfaceTypeDefinitionNode, key: number | string, parent: any): string;
481 ScalarTypeDefinition(_node: ScalarTypeDefinitionNode): string;
482 protected _buildTypeImport(identifier: string, source: string, asDefault?: boolean): string;
483 protected handleEnumValueMapper(typeIdentifier: string, importIdentifier: string | null, sourceIdentifier: string | null, sourceFile: string | null): string[];
484 getEnumsImports(): string[];
485 EnumTypeDefinition(node: EnumTypeDefinitionNode): string;
486 StringValue(node: StringValueNode): string;
487 protected makeValidEnumIdentifier(identifier: string): string;
488 protected buildEnumValuesBlock(typeName: string, values: ReadonlyArray<EnumValueDefinitionNode>): string;
489 DirectiveDefinition(_node: DirectiveDefinitionNode): string;
490 getArgumentsObjectDeclarationBlock(node: InterfaceTypeDefinitionNode | ObjectTypeDefinitionNode, name: string, field: FieldDefinitionNode): DeclarationBlock;
491 getArgumentsObjectTypeDefinition(node: InterfaceTypeDefinitionNode | ObjectTypeDefinitionNode, name: string, field: FieldDefinitionNode): string;
492 protected buildArgumentsBlock(node: InterfaceTypeDefinitionNode | ObjectTypeDefinitionNode): string;
493 protected _getScalar(name: string, type: 'input' | 'output'): string;
494 protected _getDirectiveArgumentNadInputFieldMapping(name: string): string;
495 protected _getDirectiveOverrideType(directives: ReadonlyArray<DirectiveNode>): string | null;
496 protected _getTypeForNode(node: NamedTypeNode, isVisitingInputType: boolean): string;
497 NamedType(node: NamedTypeNode, key: any, parent: any, path: any, ancestors: any): string;
498 ListType(node: ListTypeNode, _key: any, _parent: any, _path: any, _ancestors: any): string;
499 SchemaDefinition(): any;
500 getNodeComment(node: FieldDefinitionNode | EnumValueDefinitionNode | InputValueDefinitionNode): string;
501 protected getDeprecationReason(directive: DirectiveNode): string | void;
502 protected wrapWithListType(str: string): string;
503}