UNPKG

8.1 kBTypeScriptView Raw
1import { RawResolversConfig } from '@graphql-codegen/visitor-plugin-common';
2/**
3 * @description This plugin generates TypeScript signature for `resolve` functions of your GraphQL API.
4 * You can use this plugin to generate simple resolvers signature based on your GraphQL types, or you can change its behavior be providing custom model types (mappers).
5 *
6 * You can find a blog post explaining the usage of this plugin here: https://the-guild.dev/blog/better-type-safety-for-resolvers-with-graphql-codegen
7 *
8 */
9export interface TypeScriptResolversPluginConfig extends RawResolversConfig {
10 /**
11 * @description Adds an index signature to any generates resolver.
12 * @default false
13 *
14 * @exampleMarkdown
15 * ```ts filename="codegen.ts"
16 * import type { CodegenConfig } from '@graphql-codegen/cli';
17 *
18 * const config: CodegenConfig = {
19 * // ...
20 * generates: {
21 * 'path/to/file.ts': {
22 * plugins: ['typescript', 'typescript-resolvers'],
23 * config: {
24 * useIndexSignature: true
25 * },
26 * },
27 * },
28 * };
29 * export default config;
30 * ```
31 */
32 useIndexSignature?: boolean;
33 /**
34 * @description Disables/Enables Schema Stitching support.
35 * By default, the resolver signature does not include the support for schema-stitching.
36 * Set to `false` to enable that.
37 *
38 * @default true
39 * @exampleMarkdown
40 * ```ts filename="codegen.ts"
41 * import type { CodegenConfig } from '@graphql-codegen/cli';
42 *
43 * const config: CodegenConfig = {
44 * // ...
45 * generates: {
46 * 'path/to/file.ts': {
47 * plugins: ['typescript', 'typescript-resolvers'],
48 * config: {
49 * noSchemaStitching: false
50 * },
51 * },
52 * },
53 * };
54 * export default config;
55 * ```
56 */
57 noSchemaStitching?: boolean;
58 /**
59 * @description Set to `true` in order to wrap field definitions with `FieldWrapper`.
60 * This is useful to allow return types such as Promises and functions. Needed for
61 * compatibility with `federation: true` when
62 * @default true
63 */
64 wrapFieldDefinitions?: boolean;
65 /**
66 * @description You can provide your custom GraphQLResolveInfo instead of the default one from graphql-js
67 * @default "graphql#GraphQLResolveInfo"
68 *
69 * @exampleMarkdown
70 * ```ts filename="codegen.ts"
71 * import type { CodegenConfig } from '@graphql-codegen/cli';
72 *
73 * const config: CodegenConfig = {
74 * // ...
75 * generates: {
76 * 'path/to/file.ts': {
77 * plugins: ['typescript', 'typescript-resolvers'],
78 * config: {
79 * customResolveInfo: './my-types#MyResolveInfo'
80 * },
81 * },
82 * },
83 * };
84 * export default config;
85 * ```
86 */
87 customResolveInfo?: string;
88 /**
89 * @description You can provide your custom ResolveFn instead the default. It has to be a type that uses the generics `<TResult, TParent, TContext, TArgs>`
90 * @default "(parent: TParent, args: TArgs, context: TContext, info: GraphQLResolveInfo) => Promise<TResult> | TResult"
91 *
92 * @exampleMarkdown
93 * ## Custom Signature
94 *
95 * ```ts filename="codegen.ts"
96 * import type { CodegenConfig } from '@graphql-codegen/cli';
97 *
98 * const config: CodegenConfig = {
99 * // ...
100 * generates: {
101 * 'path/to/file.ts': {
102 * plugins: ['typescript', 'typescript-resolvers'],
103 * config: {
104 * customResolverFn: './my-types#MyResolveFn'
105 * },
106 * },
107 * },
108 * };
109 * export default config;
110 * ```
111 *
112 * ## With Graphile
113 *
114 * ```ts filename="codegen.ts"
115 * import type { CodegenConfig } from '@graphql-codegen/cli';
116 *
117 * const config: CodegenConfig = {
118 * // ...
119 * generates: {
120 * 'path/to/file.ts': {
121 * plugins: ['typescript', 'typescript-resolvers'],
122 * config: {
123 * customResolverFn: './my-types#MyResolveFn'
124 * },
125 * },
126 * },
127 * };
128 * export default config;
129 * ```
130 *
131 * ```ts filename="codegen.ts"
132 * import type { CodegenConfig } from '@graphql-codegen/cli';
133 *
134 * const config: CodegenConfig = {
135 * // ...
136 * generates: {
137 * "path/to/file.ts": {
138 * "plugins": [
139 * {
140 * "add": {
141 * "content": "import { GraphileHelpers } from 'graphile-utils/node8plus/fieldHelpers';"
142 * }
143 * },
144 * "typescript",
145 * "typescript-resolvers"
146 * ],
147 * "config": {
148 * "customResolverFn": "(\n parent: TParent,\n args: TArgs,\n context: TContext,\n info: GraphQLResolveInfo & { graphile: GraphileHelpers<TParent> }\n) => Promise<TResult> | TResult;\n"
149 * }
150 * }
151 * }
152 * };
153 * export default config;
154 * ```
155 *
156 */
157 customResolverFn?: string;
158 /**
159 * @description Map the usage of a directive into using a specific resolver.
160 * @exampleMarkdown
161 * ```ts filename="codegen.ts"
162 * import type { CodegenConfig } from '@graphql-codegen/cli';
163 *
164 * const config: CodegenConfig = {
165 * // ...
166 * generates: {
167 * 'path/to/file.ts': {
168 * plugins: ['typescript', 'typescript-resolvers'],
169 * config: {
170 * customResolverFn: '../resolver-types.ts#UnauthenticatedResolver',
171 * directiveResolverMappings: {
172 * authenticated: '../resolvers-types.ts#AuthenticatedResolver',
173 * },
174 * },
175 * },
176 * },
177 * };
178 * export default config;
179 * ```
180 */
181 directiveResolverMappings?: Record<string, string>;
182 /**
183 * @description Allow you to override the `ParentType` generic in each resolver, by avoid enforcing the base type of the generated generic type.
184 *
185 * This will generate `ParentType = Type` instead of `ParentType extends Type = Type` in each resolver.
186 *
187 * @exampleMarkdown
188 * ```ts filename="codegen.ts"
189 * import type { CodegenConfig } from '@graphql-codegen/cli';
190 *
191 * const config: CodegenConfig = {
192 * // ...
193 * generates: {
194 * 'path/to/file.ts': {
195 * plugins: ['typescript', 'typescript-resolvers'],
196 * config: {
197 * allowParentTypeOverride: true
198 * },
199 * },
200 * },
201 * };
202 * export default config;
203 * ```
204 *
205 */
206 allowParentTypeOverride?: boolean;
207 /**
208 * @description Sets `info` argument of resolver function to be optional field. Useful for testing.
209 *
210 * @exampleMarkdown
211 * ```ts filename="codegen.ts"
212 * import type { CodegenConfig } from '@graphql-codegen/cli';
213 *
214 * const config: CodegenConfig = {
215 * // ...
216 * generates: {
217 * 'path/to/file.ts': {
218 * plugins: ['typescript', 'typescript-resolvers'],
219 * config: {
220 * optionalInfoArgument: true
221 * },
222 * },
223 * },
224 * };
225 * export default config;
226 * ```
227 *
228 */
229 optionalInfoArgument?: boolean;
230 /**
231 * @description Set to `true` in order to allow the Resolver type to be callable
232 *
233 * @exampleMarkdown
234 * ```ts filename="codegen.ts"
235 * import type { CodegenConfig } from '@graphql-codegen/cli';
236 *
237 * const config: CodegenConfig = {
238 * // ...
239 * generates: {
240 * 'path/to/file.ts': {
241 * plugins: ['typescript', 'typescript-resolvers'],
242 * config: {
243 * makeResolverTypeCallable: true
244 * },
245 * },
246 * },
247 * };
248 * export default config;
249 * ```
250 */
251 makeResolverTypeCallable?: boolean;
252}