UNPKG

12.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';
3import { EnumValuesMap, NormalizedScalarsMap, DeclarationKindConfig, DeclarationKind, ParsedEnumValuesMap, DirectiveArgumentAndInputFieldMappings, ParsedDirectiveArgumentAndInputFieldMappings } from './types';
4import { DeclarationBlock, DeclarationBlockConfig } from './utils';
5import { OperationVariablesToObject } from './variables-to-object';
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 * ```yaml
28 * config:
29 * addUnderscoreToArgsType: true
30 * ```
31 */
32 addUnderscoreToArgsType?: boolean;
33 /**
34 * @description Overrides the default value of enum values declared in your GraphQL schema.
35 * You can also map the entire enum to an external type by providing a string that of `module#type`.
36 *
37 * @exampleMarkdown
38 * ## With Custom Values
39 * ```yaml
40 * config:
41 * enumValues:
42 * MyEnum:
43 * A: 'foo'
44 * ```
45 *
46 * ## With External Enum
47 * ```yaml
48 * config:
49 * enumValues:
50 * MyEnum: ./my-file#MyCustomEnum
51 * ```
52 *
53 * ## Import All Enums from a file
54 * ```yaml
55 * config:
56 * enumValues: ./my-file
57 * ```
58 */
59 enumValues?: EnumValuesMap;
60 /**
61 * @description Overrides the default output for various GraphQL elements.
62 *
63 * @exampleMarkdown
64 * ## Override all declarations
65 *
66 * ```yaml
67 * config:
68 * declarationKind: 'interface'
69 * ```
70 *
71 * ## Override only specific declarations
72 *
73 * ```yaml
74 * config:
75 * declarationKind:
76 * type: 'interface'
77 * input: 'interface'
78 * ```
79 */
80 declarationKind?: DeclarationKind | DeclarationKindConfig;
81 /**
82 * @default true
83 * @description Allow you to disable prefixing for generated enums, works in combination with `typesPrefix`.
84 *
85 * @exampleMarkdown
86 * ## Disable enum prefixes
87 *
88 * ```yaml
89 * config:
90 * typesPrefix: I
91 * enumPrefix: false
92 * ```
93 */
94 enumPrefix?: boolean;
95 /**
96 * @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.
97 * @default T
98 *
99 * @exampleMarkdown
100 * ## Allow Promise
101 *
102 * ```yaml
103 * generates:
104 * path/to/file.ts:
105 * plugins:
106 * - typescript
107 * config:
108 * wrapFieldDefinitions: true
109 * fieldWrapperValue: T | Promise<T>
110 * ```
111 */
112 fieldWrapperValue?: string;
113 /**
114 * @description Set to `true` in order to wrap field definitions with `FieldWrapper`.
115 * This is useful to allow return types such as Promises and functions.
116 * @default false
117 *
118 * @exampleMarkdown
119 * ## Enable wrapping fields
120 *
121 * ```yaml
122 * generates:
123 * path/to/file.ts:
124 * plugins:
125 * - typescript
126 * config:
127 * wrapFieldDefinitions: true
128 * ```
129 */
130 wrapFieldDefinitions?: boolean;
131 /**
132 * @description This will cause the generator to emit types for enums only
133 * @default false
134 *
135 * @exampleMarkdown
136 * ## Override all definition types
137 *
138 * ```yml
139 * generates:
140 * path/to/file.ts:
141 * plugins:
142 * - typescript
143 * config:
144 * onlyEnums: true
145 * ```
146 */
147 onlyEnums?: boolean;
148 /**
149 * @description This will cause the generator to emit types for operations only (basically only enums and scalars)
150 * @default false
151 *
152 * @exampleMarkdown
153 * ## Override all definition types
154 *
155 * ```yaml
156 * generates:
157 * path/to/file.ts:
158 * plugins:
159 * - typescript
160 * config:
161 * onlyOperationTypes: true
162 * ```
163 */
164 onlyOperationTypes?: boolean;
165 /**
166 * @description This will cause the generator to ignore enum values defined in GraphQLSchema
167 * @default false
168 *
169 * @exampleMarkdown
170 * ## Ignore enum values from schema
171 *
172 * ```yaml
173 * generates:
174 * path/to/file.ts:
175 * plugins:
176 * - typescript
177 * config:
178 * ignoreEnumValuesFromSchema: true
179 * ```
180 */
181 ignoreEnumValuesFromSchema?: boolean;
182 /**
183 * @name wrapEntireFieldDefinitions
184 * @type boolean
185 * @description Set to `true` in order to wrap field definitions with `EntireFieldWrapper`.
186 * This is useful to allow return types such as Promises and functions for fields.
187 * Differs from `wrapFieldDefinitions` in that this wraps the entire field definition if i.e. the field is an Array, while
188 * `wrapFieldDefinitions` will wrap every single value inside the array.
189 * @default true
190 *
191 * @example Enable wrapping entire fields
192 * ```yaml
193 * generates:
194 * path/to/file.ts:
195 * plugins:
196 * - typescript
197 * config:
198 * wrapEntireFieldDefinitions: false
199 * ```
200 */
201 wrapEntireFieldDefinitions?: boolean;
202 /**
203 * @name entireFieldWrapperValue
204 * @type string
205 * @description Allow to override the type value of `EntireFieldWrapper`. This wrapper applies outside of Array and Maybe
206 * unlike `fieldWrapperValue`, that will wrap the inner type.
207 * @default T | Promise<T> | (() => T | Promise<T>)
208 *
209 * @example Only allow values
210 * ```yaml
211 * generates:
212 * path/to/file.ts:
213 * plugins:
214 * - typescript
215 * config:
216 * entireFieldWrapperValue: T
217 * ```
218 */
219 entireFieldWrapperValue?: string;
220 /**
221 * @description Replaces a GraphQL scalar with a custom type based on the applied directive on an argument or input field.
222 *
223 * You can use both `module#type` and `module#namespace#type` syntax.
224 * Will NOT work with introspected schemas since directives are not exported.
225 * Only works with directives on ARGUMENT_DEFINITION or INPUT_FIELD_DEFINITION.
226 *
227 * **WARNING:** Using this option does only change the type definitions.
228 *
229 * 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!
230 * Otherwise, you might end up with a runtime type mismatch which could cause unnoticed bugs or runtime errors.
231 *
232 * Please use this configuration option with care!
233 *
234 * @exampleMarkdown
235 * ## Custom Context Type
236 * ```yaml
237 * plugins:
238 * config:
239 * directiveArgumentAndInputFieldMappings:
240 * AsNumber: number
241 * AsComplex: ./my-models#Complex
242 * ```
243 */
244 directiveArgumentAndInputFieldMappings?: DirectiveArgumentAndInputFieldMappings;
245 /**
246 * @description Adds a suffix to the imported names to prevent name clashes.
247 *
248 * @exampleMarkdown
249 * ```yaml
250 * plugins:
251 * config:
252 * directiveArgumentAndInputFieldMappingTypeSuffix: Model
253 * ```
254 */
255 directiveArgumentAndInputFieldMappingTypeSuffix?: string;
256}
257export declare class BaseTypesVisitor<TRawConfig extends RawTypesConfig = RawTypesConfig, TPluginConfig extends ParsedTypesConfig = ParsedTypesConfig> extends BaseVisitor<TRawConfig, TPluginConfig> {
258 protected _schema: GraphQLSchema;
259 protected _argumentsTransformer: OperationVariablesToObject;
260 constructor(_schema: GraphQLSchema, rawConfig: TRawConfig, additionalConfig: TPluginConfig, defaultScalars?: NormalizedScalarsMap);
261 protected getExportPrefix(): string;
262 getFieldWrapperValue(): string;
263 getEntireFieldWrapperValue(): string;
264 getScalarsImports(): string[];
265 getDirectiveArgumentAndInputFieldMappingsImports(): string[];
266 get scalarsDefinition(): string;
267 get directiveArgumentAndInputFieldMappingsDefinition(): string;
268 setDeclarationBlockConfig(config: DeclarationBlockConfig): void;
269 setArgumentsTransformer(argumentsTransfomer: OperationVariablesToObject): void;
270 NonNullType(node: NonNullTypeNode): string;
271 getInputObjectDeclarationBlock(node: InputObjectTypeDefinitionNode): DeclarationBlock;
272 InputObjectTypeDefinition(node: InputObjectTypeDefinitionNode): string;
273 InputValueDefinition(node: InputValueDefinitionNode): string;
274 Name(node: NameNode): string;
275 FieldDefinition(node: FieldDefinitionNode): string;
276 UnionTypeDefinition(node: UnionTypeDefinitionNode, key: string | number | undefined, parent: any): string;
277 protected mergeInterfaces(interfaces: string[], hasOtherFields: boolean): string;
278 appendInterfacesAndFieldsToBlock(block: DeclarationBlock, interfaces: string[], fields: string[]): void;
279 getObjectTypeDeclarationBlock(node: ObjectTypeDefinitionNode, originalNode: ObjectTypeDefinitionNode): DeclarationBlock;
280 protected mergeAllFields(allFields: string[], _hasInterfaces: boolean): string;
281 ObjectTypeDefinition(node: ObjectTypeDefinitionNode, key: number | string, parent: any): string;
282 getInterfaceTypeDeclarationBlock(node: InterfaceTypeDefinitionNode, _originalNode: InterfaceTypeDefinitionNode): DeclarationBlock;
283 InterfaceTypeDefinition(node: InterfaceTypeDefinitionNode, key: number | string, parent: any): string;
284 ScalarTypeDefinition(_node: ScalarTypeDefinitionNode): string;
285 protected _buildTypeImport(identifier: string, source: string, asDefault?: boolean): string;
286 protected handleEnumValueMapper(typeIdentifier: string, importIdentifier: string | null, sourceIdentifier: string | null, sourceFile: string | null): string[];
287 getEnumsImports(): string[];
288 EnumTypeDefinition(node: EnumTypeDefinitionNode): string;
289 StringValue(node: StringValueNode): string;
290 protected makeValidEnumIdentifier(identifier: string): string;
291 protected buildEnumValuesBlock(typeName: string, values: ReadonlyArray<EnumValueDefinitionNode>): string;
292 DirectiveDefinition(_node: DirectiveDefinitionNode): string;
293 getArgumentsObjectDeclarationBlock(node: InterfaceTypeDefinitionNode | ObjectTypeDefinitionNode, name: string, field: FieldDefinitionNode): DeclarationBlock;
294 getArgumentsObjectTypeDefinition(node: InterfaceTypeDefinitionNode | ObjectTypeDefinitionNode, name: string, field: FieldDefinitionNode): string;
295 protected buildArgumentsBlock(node: InterfaceTypeDefinitionNode | ObjectTypeDefinitionNode): string;
296 protected _getScalar(name: string): string;
297 protected _getDirectiveArgumentNadInputFieldMapping(name: string): string;
298 protected _getDirectiveOverrideType(directives: ReadonlyArray<DirectiveNode>): string | null;
299 protected _getTypeForNode(node: NamedTypeNode): string;
300 NamedType(node: NamedTypeNode, key: any, parent: any, path: any, ancestors: any): string;
301 ListType(node: ListTypeNode, key: any, parent: any, path: any, ancestors: any): string;
302 SchemaDefinition(): any;
303 getNodeComment(node: FieldDefinitionNode | EnumValueDefinitionNode): string;
304 protected getDeprecationReason(directive: DirectiveNode): string | void;
305 protected wrapWithListType(str: string): string;
306}