UNPKG

14.5 kBTypeScriptView Raw
1import { ParsedConfig, RawConfig, BaseVisitor, BaseVisitorConvertOptions } from './base-visitor';
2import { NormalizedScalarsMap, EnumValuesMap, ParsedEnumValuesMap, DeclarationKind, ConvertOptions, AvoidOptionalsConfig } from './types';
3import { DeclarationBlockConfig } from './utils';
4import { NameNode, ListTypeNode, NamedTypeNode, FieldDefinitionNode, ObjectTypeDefinitionNode, GraphQLSchema, NonNullTypeNode, UnionTypeDefinitionNode, ScalarTypeDefinitionNode, InterfaceTypeDefinitionNode, GraphQLNamedType, DirectiveDefinitionNode, InputValueDefinitionNode, EnumTypeDefinitionNode, ASTNode } from 'graphql';
5import { OperationVariablesToObject } from './variables-to-object';
6import { ParsedMapper } from './mappers';
7import { ApolloFederation } from '@graphql-codegen/plugin-helpers';
8export interface ParsedResolversConfig extends ParsedConfig {
9 contextType: ParsedMapper;
10 fieldContextTypes: Array<string>;
11 rootValueType: ParsedMapper;
12 mappers: {
13 [typeName: string]: ParsedMapper;
14 };
15 defaultMapper: ParsedMapper | null;
16 avoidOptionals: AvoidOptionalsConfig;
17 addUnderscoreToArgsType: boolean;
18 enumValues: ParsedEnumValuesMap;
19 resolverTypeWrapperSignature: string;
20 federation: boolean;
21 enumPrefix: boolean;
22 optionalResolveType: boolean;
23 immutableTypes: boolean;
24 namespacedImportName: string;
25 resolverTypeSuffix: string;
26 allResolversTypeName: string;
27 internalResolversPrefix: string;
28 onlyResolveTypeForInterfaces: boolean;
29}
30export interface RawResolversConfig extends RawConfig {
31 /**
32 * @description Adds `_` to generated `Args` types in order to avoid duplicate identifiers.
33 *
34 * @exampleMarkdown
35 * ```yml
36 * config:
37 * addUnderscoreToArgsType: true
38 * ```
39 *
40 */
41 addUnderscoreToArgsType?: boolean;
42 /**
43 * @description Use this configuration to set a custom type for your `context`, and it will
44 * effect all the resolvers, without the need to override it using generics each time.
45 * If you wish to use an external type and import it from another file, you can use `add` plugin
46 * and add the required `import` statement, or you can use a `module#type` syntax.
47 *
48 * @exampleMarkdown
49 * ## Custom Context Type
50 * ```yml
51 * plugins
52 * config:
53 * contextType: MyContext
54 * ```
55 *
56 * ## Custom Context Type
57 * ```yml
58 * plugins
59 * config:
60 * contextType: ./my-types#MyContext
61 * ```
62 */
63 contextType?: string;
64 /**
65 * @description Use this to set a custom type for a specific field `context`.
66 * It will only affect the targeted resolvers.
67 * You can either use `Field.Path#ContextTypeName` or `Field.Path#ExternalFileName#ContextTypeName`
68 *
69 * @exampleMarkdown
70 * ## Custom Field Context Types
71 * ```
72 * plugins
73 * config:
74 * fieldContextTypes:
75 * - MyType.foo#CustomContextType
76 * - MyType.bar#./my-file#ContextTypeOne
77 * ```
78 *
79 */
80 fieldContextTypes?: Array<string>;
81 /**
82 * @description Use this configuration to set a custom type for the `rootValue`, and it will
83 * effect resolvers of all root types (Query, Mutation and Subscription), without the need to override it using generics each time.
84 * If you wish to use an external type and import it from another file, you can use `add` plugin
85 * and add the required `import` statement, or you can use both `module#type` or `module#namespace#type` syntax.
86 *
87 * @exampleMarkdown
88 * ## Custom RootValue Type
89 * ```yml
90 * plugins
91 * config:
92 * rootValueType: MyRootValue
93 * ```
94 * ## Custom RootValue Type
95 * ```yml
96 * plugins
97 * config:
98 * rootValueType: ./my-types#MyRootValue
99 * ```
100 */
101 rootValueType?: string;
102 /**
103 * @description Adds a suffix to the imported names to prevent name clashes.
104 *
105 * @exampleMarkdown
106 * ```yml
107 * plugins
108 * config:
109 * mapperTypeSuffix: Model
110 * ```
111 */
112 mapperTypeSuffix?: string;
113 /**
114 * @description Replaces a GraphQL type usage with a custom type, allowing you to return custom object from
115 * your resolvers.
116 * You can use both `module#type` and `module#namespace#type` syntax.
117 *
118 * @exampleMarkdown
119 * ## Custom Context Type
120 * ```yml
121 * plugins
122 * config:
123 * mappers:
124 * User: ./my-models#UserDbObject
125 * Book: ./my-models#Collections#Book
126 * ```
127 */
128 mappers?: {
129 [typeName: string]: string;
130 };
131 /**
132 * @description Allow you to set the default mapper when it's not being override by `mappers` or generics.
133 * You can specify a type name, or specify a string in `module#type` or `module#namespace#type` format.
134 * The default value of mappers it the TypeScript type generated by `typescript` package.
135 *
136 * @exampleMarkdown
137 * ## Replace with any
138 * ```yml
139 * plugins
140 * config:
141 * defaultMapper: any
142 * ```
143 *
144 * ## Custom Base Object
145 * ```yml
146 * plugins
147 * config:
148 * defaultMapper: ./my-file#BaseObject
149 * ```
150 *
151 * ## Wrap default types with Partial
152 * You can also specify a custom wrapper for the original type, without overriding the original generated types, use "{T}" to specify the identifier. (for flow, use `$Shape<{T}>`)
153 * ```yml
154 * plugins
155 * config:
156 * defaultMapper: Partial<{T}>
157 * ```
158 *
159 * ## Allow deep partial with `utility-types`
160 * ```yml
161 * plugins
162 * plugins:
163 * - "typescript"
164 * - "typescript-resolvers"
165 * - add:
166 * content: "import { DeepPartial } from 'utility-types';"
167 * config:
168 * defaultMapper: DeepPartial<{T}>
169 * ```
170 */
171 defaultMapper?: string;
172 /**
173 * @description This will cause the generator to avoid using optionals (`?`),
174 * so all field resolvers must be implemented in order to avoid compilation errors.
175 * @default false
176 *
177 * @exampleMarkdown
178 * ## Override all definition types
179 * ```yml
180 * generates:
181 * path/to/file.ts:
182 * plugins:
183 * - typescript
184 * - typescript-resolvers
185 * config:
186 * avoidOptionals: true
187 * ```
188 *
189 * ## Override only specific definition types
190 * ```yml
191 * generates:
192 * path/to/file.ts:
193 * plugins:
194 * - typescript
195 * config:
196 * avoidOptionals:
197 * field: true
198 * inputValue: true
199 * object: true
200 * defaultValue: true
201 * ```
202 */
203 avoidOptionals?: boolean | AvoidOptionalsConfig;
204 /**
205 * @description Warns about unused mappers.
206 * @default true
207 *
208 * @exampleMarkdown
209 * ```yml
210 * generates:
211 * path/to/file.ts:
212 * plugins:
213 * - typescript
214 * - typescript-resolvers
215 * config:
216 * showUnusedMappers: true
217 * ```
218 */
219 showUnusedMappers?: boolean;
220 /**
221 * @description Overrides the default value of enum values declared in your GraphQL schema, supported
222 * in this plugin because of the need for integration with `typescript` package.
223 * See documentation under `typescript` plugin for more information and examples.
224 */
225 enumValues?: EnumValuesMap;
226 /**
227 * @default Promise<T> | T
228 * @description Allow you to override `resolverTypeWrapper` definition.
229 */
230 resolverTypeWrapperSignature?: string;
231 /**
232 * @default false
233 * @description Supports Apollo Federation
234 */
235 federation?: boolean;
236 /**
237 * @default true
238 * @description Allow you to disable prefixing for generated enums, works in combination with `typesPrefix`.
239 *
240 * @exampleMarkdown
241 * ## Disable enum prefixes
242 * ```yml
243 * config:
244 * typesPrefix: I
245 * enumPrefix: false
246 * ```
247 */
248 enumPrefix?: boolean;
249 /**
250 * @default false
251 * @description Sets the `__resolveType` field as optional field.
252 */
253 optionalResolveType?: boolean;
254 /**
255 * @default false
256 * @description Generates immutable types by adding `readonly` to properties and uses `ReadonlyArray`.
257 */
258 immutableTypes?: boolean;
259 /**
260 * @default ''
261 * @description Prefixes all GraphQL related generated types with that value, as namespaces import.
262 * You can use this featuere to allow seperation of plugins to different files.
263 */
264 namespacedImportName?: string;
265 /**
266 * @default Resolvers
267 * @description Suffix we add to each generated type resolver.
268 */
269 resolverTypeSuffix?: string;
270 /**
271 * @default Resolvers
272 * @description The type name to use when exporting all resolvers signature as unified type.
273 */
274 allResolversTypeName?: string;
275 /**
276 * @type string
277 * @default '__'
278 * @description Defines the prefix value used for `__resolveType` and and `__isTypeOf` resolvers.
279 * If you are using `mercurius-js`, please set this field to empty string for better compatiblity.
280 */
281 internalResolversPrefix?: string;
282 /**
283 * @type boolean
284 * @default false
285 * @description Turning this flag to `true` will generate resolver siganture that has only `resolveType` for interfaces, forcing developers to write inherited type resolvers in the type itself.
286 */
287 onlyResolveTypeForInterfaces?: boolean;
288}
289export declare type ResolverTypes = {
290 [gqlType: string]: string;
291};
292export declare type ResolverParentTypes = {
293 [gqlType: string]: string;
294};
295export declare type GroupedMappers = Record<string, {
296 identifier: string;
297 asDefault?: boolean;
298}[]>;
299declare type FieldContextTypeMap = Record<string, ParsedMapper>;
300export declare class BaseResolversVisitor<TRawConfig extends RawResolversConfig = RawResolversConfig, TPluginConfig extends ParsedResolversConfig = ParsedResolversConfig> extends BaseVisitor<TRawConfig, TPluginConfig> {
301 private _schema;
302 protected _parsedConfig: TPluginConfig;
303 protected _declarationBlockConfig: DeclarationBlockConfig;
304 protected _collectedResolvers: {
305 [key: string]: string;
306 };
307 protected _collectedDirectiveResolvers: {
308 [key: string]: string;
309 };
310 protected _variablesTransfomer: OperationVariablesToObject;
311 protected _usedMappers: {
312 [key: string]: boolean;
313 };
314 protected _resolversTypes: ResolverTypes;
315 protected _resolversParentTypes: ResolverParentTypes;
316 protected _rootTypeNames: string[];
317 protected _globalDeclarations: Set<string>;
318 protected _federation: ApolloFederation;
319 protected _hasScalars: boolean;
320 protected _hasFederation: boolean;
321 protected _fieldContextTypeMap: FieldContextTypeMap;
322 constructor(rawConfig: TRawConfig, additionalConfig: TPluginConfig, _schema: GraphQLSchema, defaultScalars?: NormalizedScalarsMap);
323 getResolverTypeWrapperSignature(): string;
324 protected shouldMapType(type: GraphQLNamedType, checkedBefore?: {
325 [typeName: string]: boolean;
326 }, duringCheck?: string[]): boolean;
327 convertName(node: ASTNode | string, options?: BaseVisitorConvertOptions & ConvertOptions, applyNamespacedImport?: boolean): string;
328 protected createResolversFields(applyWrapper: (str: string) => string, clearWrapper: (str: string) => string, getTypeToUse: (str: string) => string, shouldInclude?: (type: GraphQLNamedType) => boolean): ResolverTypes;
329 protected replaceFieldsInType(typeName: string, relevantFields: {
330 addOptionalSign: boolean;
331 fieldName: string;
332 replaceWithType: string;
333 }[]): string;
334 protected applyMaybe(str: string): string;
335 protected applyResolverTypeWrapper(str: string): string;
336 protected clearMaybe(str: string): string;
337 protected clearResolverTypeWrapper(str: string): string;
338 protected wrapWithArray(t: string): string;
339 protected createFieldContextTypeMap(): FieldContextTypeMap;
340 buildResolversTypes(): string;
341 buildResolversParentTypes(): string;
342 get schema(): GraphQLSchema;
343 get defaultMapperType(): string;
344 get unusedMappers(): string[];
345 get globalDeclarations(): string[];
346 protected isMapperImported(groupedMappers: GroupedMappers, identifier: string, source: string): boolean;
347 get mappersImports(): string[];
348 setDeclarationBlockConfig(config: DeclarationBlockConfig): void;
349 setVariablesTransformer(variablesTransfomer: OperationVariablesToObject): void;
350 hasScalars(): boolean;
351 hasFederation(): boolean;
352 getRootResolver(): string;
353 protected formatRootResolver(schemaTypeName: string, resolverType: string, declarationKind: DeclarationKind): string;
354 getAllDirectiveResolvers(): string;
355 Name(node: NameNode): string;
356 ListType(node: ListTypeNode): string;
357 protected _getScalar(name: string): string;
358 NamedType(node: NamedTypeNode): string;
359 NonNullType(node: NonNullTypeNode): string;
360 protected markMapperAsUsed(name: string): void;
361 protected getTypeToUse(name: string): string;
362 protected getParentTypeToUse(name: string): string;
363 protected getParentTypeForSignature(_node: FieldDefinitionNode): string;
364 protected transformParentGenericType(parentType: string): string;
365 FieldDefinition(node: FieldDefinitionNode, key: string | number, parent: any): (parentName: string) => string | null;
366 protected applyRequireFields(argsType: string, fields: InputValueDefinitionNode[]): string;
367 protected applyOptionalFields(argsType: string, _fields: readonly InputValueDefinitionNode[]): string;
368 ObjectTypeDefinition(node: ObjectTypeDefinitionNode): string;
369 UnionTypeDefinition(node: UnionTypeDefinitionNode, key: string | number, parent: any): string;
370 ScalarTypeDefinition(node: ScalarTypeDefinitionNode): string;
371 DirectiveDefinition(node: DirectiveDefinitionNode, key: string | number, parent: any): string;
372 protected buildEnumResolverContentBlock(_node: EnumTypeDefinitionNode, _mappedEnumType: string): string;
373 protected buildEnumResolversExplicitMappedValues(_node: EnumTypeDefinitionNode, _valuesMapping: {
374 [valueName: string]: string | number;
375 }): string;
376 EnumTypeDefinition(node: EnumTypeDefinitionNode): string;
377 InterfaceTypeDefinition(node: InterfaceTypeDefinitionNode): string;
378 SchemaDefinition(): any;
379}
380export {};