UNPKG

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