UNPKG

9.71 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 } 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 onlyOperationTypes: boolean;
11 enumPrefix: boolean;
12 fieldWrapperValue: string;
13 wrapFieldDefinitions: boolean;
14 entireFieldWrapperValue: string;
15 wrapEntireDefinitions: boolean;
16 ignoreEnumValuesFromSchema: boolean;
17}
18export interface RawTypesConfig extends RawConfig {
19 /**
20 * @description Adds `_` to generated `Args` types in order to avoid duplicate identifiers.
21 *
22 * @exampleMarkdown
23 * ## With Custom Values
24 * ```yml
25 * config:
26 * addUnderscoreToArgsType: true
27 * ```
28 */
29 addUnderscoreToArgsType?: boolean;
30 /**
31 * @description Overrides the default value of enum values declared in your GraphQL schema.
32 * You can also map the entire enum to an external type by providing a string that of `module#type`.
33 *
34 * @exampleMarkdown
35 * ## With Custom Values
36 * ```yml
37 * config:
38 * enumValues:
39 * MyEnum:
40 * A: 'foo'
41 * ```
42 *
43 * ## With External Enum
44 * ```yml
45 * config:
46 * enumValues:
47 * MyEnum: ./my-file#MyCustomEnum
48 * ```
49 *
50 * ## Import All Enums from a file
51 * ```yml
52 * config:
53 * enumValues: ./my-file
54 * ```
55 */
56 enumValues?: EnumValuesMap;
57 /**
58 * @description Overrides the default output for various GraphQL elements.
59 *
60 * @exampleMarkdown
61 * ## Override all declarations
62 * ```yml
63 * config:
64 * declarationKind: 'interface'
65 * ```
66 *
67 * ## Override only specific declarations
68 * ```yml
69 * config:
70 * declarationKind:
71 * type: 'interface'
72 * input: 'interface'
73 * ```
74 */
75 declarationKind?: DeclarationKind | DeclarationKindConfig;
76 /**
77 * @default true
78 * @description Allow you to disable prefixing for generated enums, works in combination with `typesPrefix`.
79 *
80 * @exampleMarkdown
81 * ## Disable enum prefixes
82 * ```yml
83 * config:
84 * typesPrefix: I
85 * enumPrefix: false
86 * ```
87 */
88 enumPrefix?: boolean;
89 /**
90 * @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.
91 * @default T
92 *
93 * @exampleMarkdown
94 * ## Allow Promise
95 * ```yml
96 * generates:
97 * path/to/file.ts:
98 * plugins:
99 * - typescript
100 * config:
101 * wrapFieldDefinitions: true
102 * fieldWrapperValue: T | Promise<T>
103 * ```
104 */
105 fieldWrapperValue?: string;
106 /**
107 * @description Set the to `true` in order to wrap field definitions with `FieldWrapper`.
108 * This is useful to allow return types such as Promises and functions.
109 * @default false
110 *
111 * @exampleMarkdown
112 * ## Enable wrapping fields
113 * ```yml
114 * generates:
115 * path/to/file.ts:
116 * plugins:
117 * - typescript
118 * config:
119 * wrapFieldDefinitions: true
120 * ```
121 */
122 wrapFieldDefinitions?: boolean;
123 /**
124 * @description This will cause the generator to emit types for operations only (basically only enums and scalars)
125 * @default false
126 *
127 * @exampleMarkdown
128 * ## Override all definition types
129 * ```yml
130 * generates:
131 * path/to/file.ts:
132 * plugins:
133 * - typescript
134 * config:
135 * onlyOperationTypes: true
136 * ```
137 */
138 onlyOperationTypes?: boolean;
139 /**
140 * @description This will cause the generator to ignore enum values defined in GraphQLSchema
141 * @default false
142 *
143 * @exampleMarkdown
144 * ## Ignore enum values from schema
145 * ```yml
146 * generates:
147 * path/to/file.ts:
148 * plugins:
149 * - typescript
150 * config:
151 * ignoreEnumValuesFromSchema: true
152 * ```
153 */
154 ignoreEnumValuesFromSchema?: boolean;
155 /**
156 * @name wrapEntireFieldDefinitions
157 * @type boolean
158 * @description Set the to `true` in order to wrap field definitions with `EntireFieldWrapper`.
159 * This is useful to allow return types such as Promises and functions for fields.
160 * Differs from `wrapFieldDefinitions` in that this wraps the entire field definition if ie. the field is an Array, while
161 * `wrapFieldDefinitions` will wrap every single value inside the array.
162 * @default true
163 *
164 * @example Enable wrapping entire fields
165 * ```yml
166 * generates:
167 * path/to/file.ts:
168 * plugins:
169 * - typescript
170 * config:
171 * wrapEntireFieldDefinitions: false
172 * ```
173 */
174 wrapEntireFieldDefinitions?: boolean;
175 /**
176 * @name entireFieldWrapperValue
177 * @type string
178 * @description Allow to override the type value of `EntireFieldWrapper`. This wrapper applies outside of Array and Maybe
179 * unlike `fieldWrapperValue`, that will wrap the inner type.
180 * @default T | Promise<T> | (() => T | Promise<T>)
181 *
182 * @example Only allow values
183 * ```yml
184 * generates:
185 * path/to/file.ts:
186 * plugins:
187 * - typescript
188 * config:
189 * entireFieldWrapperValue: T
190 * ```
191 */
192 entireFieldWrapperValue?: string;
193}
194export declare class BaseTypesVisitor<TRawConfig extends RawTypesConfig = RawTypesConfig, TPluginConfig extends ParsedTypesConfig = ParsedTypesConfig> extends BaseVisitor<TRawConfig, TPluginConfig> {
195 protected _schema: GraphQLSchema;
196 protected _argumentsTransformer: OperationVariablesToObject;
197 constructor(_schema: GraphQLSchema, rawConfig: TRawConfig, additionalConfig: TPluginConfig, defaultScalars?: NormalizedScalarsMap);
198 protected getExportPrefix(): string;
199 getFieldWrapperValue(): string;
200 getEntireFieldWrapperValue(): string;
201 getScalarsImports(): string[];
202 get scalarsDefinition(): string;
203 setDeclarationBlockConfig(config: DeclarationBlockConfig): void;
204 setArgumentsTransformer(argumentsTransfomer: OperationVariablesToObject): void;
205 NonNullType(node: NonNullTypeNode): string;
206 getInputObjectDeclarationBlock(node: InputObjectTypeDefinitionNode): DeclarationBlock;
207 InputObjectTypeDefinition(node: InputObjectTypeDefinitionNode): string;
208 InputValueDefinition(node: InputValueDefinitionNode): string;
209 Name(node: NameNode): string;
210 FieldDefinition(node: FieldDefinitionNode): string;
211 UnionTypeDefinition(node: UnionTypeDefinitionNode, key: string | number | undefined, parent: any): string;
212 protected mergeInterfaces(interfaces: string[], hasOtherFields: boolean): string;
213 appendInterfacesAndFieldsToBlock(block: DeclarationBlock, interfaces: string[], fields: string[]): void;
214 getObjectTypeDeclarationBlock(node: ObjectTypeDefinitionNode, originalNode: ObjectTypeDefinitionNode): DeclarationBlock;
215 getFieldComment(node: FieldDefinitionNode): string;
216 protected mergeAllFields(allFields: string[], _hasInterfaces: boolean): string;
217 ObjectTypeDefinition(node: ObjectTypeDefinitionNode, key: number | string, parent: any): string;
218 getInterfaceTypeDeclarationBlock(node: InterfaceTypeDefinitionNode, _originalNode: InterfaceTypeDefinitionNode): DeclarationBlock;
219 InterfaceTypeDefinition(node: InterfaceTypeDefinitionNode, key: number | string, parent: any): string;
220 ScalarTypeDefinition(_node: ScalarTypeDefinitionNode): string;
221 protected _buildTypeImport(identifier: string, source: string, asDefault?: boolean): string;
222 protected handleEnumValueMapper(typeIdentifier: string, importIdentifier: string | null, sourceIdentifier: string | null, sourceFile: string | null): string[];
223 getEnumsImports(): string[];
224 EnumTypeDefinition(node: EnumTypeDefinitionNode): string;
225 StringValue(node: StringValueNode): string;
226 protected makeValidEnumIdentifier(identifier: string): string;
227 protected buildEnumValuesBlock(typeName: string, values: ReadonlyArray<EnumValueDefinitionNode>): string;
228 DirectiveDefinition(_node: DirectiveDefinitionNode): string;
229 getArgumentsObjectDeclarationBlock(node: InterfaceTypeDefinitionNode | ObjectTypeDefinitionNode, name: string, field: FieldDefinitionNode): DeclarationBlock;
230 getArgumentsObjectTypeDefinition(node: InterfaceTypeDefinitionNode | ObjectTypeDefinitionNode, name: string, field: FieldDefinitionNode): string;
231 protected buildArgumentsBlock(node: InterfaceTypeDefinitionNode | ObjectTypeDefinitionNode): string;
232 protected _getScalar(name: string): string;
233 protected _getTypeForNode(node: NamedTypeNode): string;
234 NamedType(node: NamedTypeNode, key: any, parent: any, path: any, ancestors: any): string;
235 ListType(node: ListTypeNode): string;
236 SchemaDefinition(): any;
237 protected getDeprecationReason(directive: DirectiveNode): string | void;
238 protected wrapWithListType(str: string): string;
239}