UNPKG

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