UNPKG

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