UNPKG

25.4 kBTypeScriptView Raw
1import { GraphQLSchema, DocumentNode } from 'graphql';
2import { Source } from '@graphql-tools/utils';
3import type { Profiler } from './profiler.js';
4import type { ApolloEngineOptions } from '@graphql-tools/apollo-engine-loader';
5export declare namespace Types {
6 interface GenerateOptions {
7 filename: string;
8 plugins: Types.ConfiguredPlugin[];
9 schema: DocumentNode;
10 schemaAst?: GraphQLSchema;
11 documents: Types.DocumentFile[];
12 config: {
13 [key: string]: any;
14 };
15 pluginMap: {
16 [name: string]: CodegenPlugin;
17 };
18 skipDocumentsValidation?: Types.SkipDocumentsValidationOptions;
19 pluginContext?: {
20 [key: string]: any;
21 };
22 profiler?: Profiler;
23 cache?<T>(namespace: string, key: string, factory: () => Promise<T>): Promise<T>;
24 }
25 type FileOutput = {
26 filename: string;
27 content: string;
28 hooks?: {
29 beforeOneFileWrite?: LifecycleHooksDefinition['beforeOneFileWrite'];
30 afterOneFileWrite?: LifecycleHooksDefinition['afterOneFileWrite'];
31 };
32 };
33 interface DocumentFile extends Source {
34 hash?: string;
35 }
36 type Promisable<T> = T | Promise<T>;
37 type InstanceOrArray<T> = T | T[];
38 /**
39 * @additionalProperties false
40 * @description Loads schema using a pointer, with a custom loader (code file).
41 */
42 interface SchemaWithLoaderOptions {
43 /**
44 * @description Specify a path to a custom code file (local or module) that will handle the schema loading.
45 */
46 loader: string;
47 }
48 interface SchemaWithLoader {
49 [pointer: string]: SchemaWithLoaderOptions;
50 }
51 /**
52 * @additionalProperties false
53 * @description Loads schema using a pointer, without using `require` while looking for schemas in code files.
54 */
55 interface SchemaFromCodeFileOptions {
56 /**
57 * @description Set this to `true` in order to tell codegen not to try to `require` files in order to find schema/docs
58 */
59 noRequire?: boolean;
60 /**
61 * @description Set this to `true` in order to tell codegen not to try to extract GraphQL AST strings schema/docs
62 */
63 noPluck?: boolean;
64 /**
65 * @description Set this to `true` in order to tell codegen to skip documents validation.
66 */
67 assumeValid?: boolean;
68 }
69 interface SchemaFromCodeFile {
70 [path: string]: SchemaFromCodeFileOptions;
71 }
72 /**
73 * @additionalProperties false
74 * @description Loads a schema from remote endpoint, with custom http options.
75 */
76 interface UrlSchemaOptions {
77 /**
78 * @description HTTP headers you wish to add to the HTTP request sent by codegen to fetch your GraphQL remote schema.
79 */
80 headers?: {
81 [headerName: string]: string;
82 };
83 /**
84 * @description Specify a Node module name, or a custom file, to be used instead of standard `fetch`
85 */
86 customFetch?: string;
87 /**
88 * @description HTTP Method to use, either POST (default) or GET.
89 */
90 method?: string;
91 }
92 interface UrlSchemaWithOptions {
93 [url: string]: UrlSchemaOptions;
94 }
95 /**
96 * @additionalProperties false
97 * @description Loads a schema a local file or files, with customized options for parsing/loading.
98 */
99 interface LocalSchemaPathOptions {
100 /**
101 * @description Skips checks for graphql-import syntax and loads the file as-is, without imports support.
102 * @default true
103 */
104 skipGraphQLImport?: boolean;
105 /**
106 * @description Converts all GraphQL comments (`#` sign) to descriptions during the parse phase, to make it available
107 * for plugins later.
108 * @default false
109 */
110 commentDescriptions?: boolean;
111 /**
112 * Set to true to assume the SDL is valid.
113 *
114 * @default false
115 */
116 assumeValidSDL?: boolean;
117 /**
118 * By default, the parser creates AST nodes that know the location
119 * in the source that they correspond to. This configuration flag
120 * disables that behavior for performance or testing.
121 *
122 * @default false
123 */
124 noLocation?: boolean;
125 /**
126 * If enabled, the parser will parse empty fields sets in the Schema
127 * Definition Language. Otherwise, the parser will follow the current
128 * specification.
129 *
130 * This option is provided to ease adoption of the final SDL specification
131 * and will be removed in v16.
132 *
133 * @default false
134 */
135 allowLegacySDLEmptyFields?: boolean;
136 /**
137 * If enabled, the parser will parse implemented interfaces with no `&`
138 * character between each interface. Otherwise, the parser will follow the
139 * current specification.
140 *
141 * This option is provided to ease adoption of the final SDL specification
142 * and will be removed in v16.
143 *
144 * @default false
145 */
146 allowLegacySDLImplementsInterfaces?: boolean;
147 /**
148 * EXPERIMENTAL:
149 *
150 * If enabled, the parser will understand and parse variable definitions
151 * contained in a fragment definition. They'll be represented in the
152 * `variableDefinitions` field of the FragmentDefinitionNode.
153 *
154 * The syntax is identical to normal, query-defined variables. For example:
155 *
156 * fragment A($var: Boolean = false) on T {
157 * ...
158 * }
159 *
160 * Note: this feature is experimental and may change or be removed in the
161 * future.
162 *
163 * @default false
164 */
165 experimentalFragmentVariables?: boolean;
166 }
167 interface LocalSchemaPathWithOptions {
168 [globPath: string]: LocalSchemaPathOptions;
169 }
170 interface ApolloEngineSchemaOptions {
171 'apollo-engine': ApolloEngineOptions;
172 }
173 type SchemaGlobPath = string;
174 /**
175 * @description A URL to your GraphQL endpoint, a local path to `.graphql` file, a glob pattern to your GraphQL schema files, or a JavaScript file that exports the schema to generate code from. This can also be an array which specifies multiple schemas to generate code from. You can read more about the supported formats [here](schema-field#available-formats).
176 */
177 type Schema = string | UrlSchemaWithOptions | ApolloEngineSchemaOptions | LocalSchemaPathWithOptions | SchemaGlobPath | SchemaWithLoader | SchemaFromCodeFile;
178 type OperationDocumentGlobPath = string;
179 /**
180 * @additionalProperties false
181 * @description Specify a path to a custom loader for your GraphQL documents.
182 */
183 interface CustomDocumentLoaderOptions {
184 /**
185 * @description Specify a path to a custom code file (local or module) that will handle the documents loading.
186 */
187 loader: string;
188 }
189 interface CustomDocumentLoader {
190 [path: string]: CustomDocumentLoaderOptions;
191 }
192 type OperationDocument = OperationDocumentGlobPath | CustomDocumentLoader;
193 type PluginConfig<T = any> = {
194 [key: string]: T;
195 };
196 interface ConfiguredPlugin {
197 [name: string]: PluginConfig;
198 }
199 type NamedPlugin = string;
200 type NamedPreset = string;
201 type OutputConfig = NamedPlugin | ConfiguredPlugin;
202 type PresetNamesBase = 'client' | 'near-operation-file' | 'gql-tag-operations' | 'graphql-modules' | 'import-types';
203 type PresetNames = `${PresetNamesBase}-preset` | PresetNamesBase;
204 /**
205 * @additionalProperties false
206 */
207 interface ConfiguredOutput {
208 /**
209 * @type array
210 * @items { "$ref": "#/definitions/GeneratedPluginsMap" }
211 * @description List of plugins to apply to this current output file.
212 *
213 * You can either specify plugins from the community using the NPM package name (after you installed it in your project), or you can use a path to a local file for custom plugins.
214 *
215 * You can find a list of available plugins here: https://graphql-code-generator.com/docs/plugins/index
216 * Need a custom plugin? read this: https://graphql-code-generator.com/docs/custom-codegen/index
217 */
218 plugins: OutputConfig[];
219 /**
220 * @description If your setup uses Preset to have a more dynamic setup and output, set the name of your preset here.
221 *
222 * Presets are a way to have more than one file output, for example: https://graphql-code-generator.com/docs/presets/near-operation-file
223 *
224 * You can either specify a preset from the community using the NPM package name (after you installed it in your project), or you can use a path to a local file for a custom preset.
225 *
226 * List of available presets: https://graphql-code-generator.com/docs/presets/presets-index
227 */
228 preset?: PresetNames | OutputPreset;
229 /**
230 * @description If your setup uses Preset to have a more dynamic setup and output, set the configuration object of your preset here.
231 *
232 * List of available presets: https://graphql-code-generator.com/docs/presets/presets-index
233 */
234 presetConfig?: {
235 [key: string]: any;
236 };
237 /**
238 * @description A flag to overwrite files if they already exist when generating code (`true` by default).
239 *
240 * For more details: https://graphql-code-generator.com/docs/config-reference/codegen-config
241 */
242 overwrite?: boolean;
243 /**
244 * @description A pointer(s) to your GraphQL documents: query, mutation, subscription and fragment. These documents will be loaded into for all your output files.
245 * You can use one of the following:
246 *
247 * - Path to a local `.graphql` file
248 * - Path to a code file (for example: `.js` or `.tsx`) containing GraphQL operation strings.
249 * - Glob expression pointing to multiple `.graphql` files
250 * - Glob expression pointing to multiple code files
251 * - Inline string containing GraphQL SDL operation definition
252 *
253 * You can specify either a single file, or multiple.
254 *
255 * For more details: https://graphql-code-generator.com/docs/config-reference/documents-field
256 */
257 documents?: InstanceOrArray<OperationDocument>;
258 /**
259 * @description A pointer(s) to your GraphQL schema. This schema will be available only for this specific `generates` record.
260 * You can use one of the following:
261 *
262 * - URL pointing to a GraphQL endpoint
263 * - Path to a local `.json` file
264 * - Path to a local `.graphql` file
265 * - Glob expression pointing to multiple `.graphql` files
266 * - Path to a local code file (for example: `.js`) that exports `GraphQLSchema` object
267 * - Inline string containing GraphQL SDL schema definition
268 *
269 * You can specify either a single schema, or multiple, and GraphQL Code Generator will merge the schemas into a single schema.
270 *
271 * For more details: https://graphql-code-generator.com/docs/config-reference/schema-field
272 */
273 schema?: InstanceOrArray<Schema>;
274 /**
275 * @description Configuration object containing key => value that will be passes to the plugins.
276 * Specifying configuration in this level of your configuration file will pass it to all plugins, in all outputs.
277 *
278 * The options may vary depends on what plugins you are using.
279 *
280 * For more details: https://graphql-code-generator.com/docs/config-reference/config-field
281 */
282 config?: PluginConfig;
283 /**
284 * @description Specifies scripts to run when events are happening in the codegen core.
285 * Hooks defined on that level will effect only the current output files.
286 *
287 * For more details: https://graphql-code-generator.com/docs/config-reference/lifecycle-hooks
288 */
289 hooks?: Partial<LifecycleHooksDefinition>;
290 }
291 type PresetFnArgs<Config = any, PluginConfig = {
292 [key: string]: any;
293 }> = {
294 presetConfig: Config;
295 baseOutputDir: string;
296 plugins: Types.ConfiguredPlugin[];
297 schema: DocumentNode;
298 schemaAst?: GraphQLSchema;
299 documents: Types.DocumentFile[];
300 config: PluginConfig;
301 pluginMap: {
302 [name: string]: CodegenPlugin;
303 };
304 pluginContext?: {
305 [name: string]: any;
306 };
307 profiler?: Profiler;
308 cache?<T>(namespace: string, key: string, factory: () => Promise<T>): Promise<T>;
309 };
310 type OutputPreset<TPresetConfig = any> = {
311 buildGeneratesSection: (options: PresetFnArgs<TPresetConfig>) => Promisable<GenerateOptions[]>;
312 prepareDocuments?: (outputFilePath: string, outputSpecificDocuments: Types.OperationDocument[]) => Promisable<Types.OperationDocument[]>;
313 };
314 type RequireExtension = InstanceOrArray<string>;
315 type PackageLoaderFn<TExpectedResult> = (name: string) => Promisable<TExpectedResult>;
316 /**
317 * @description Represents the root YAML schema for the config file.
318 * @additionalProperties false
319 */
320 interface Config {
321 /**
322 * @description A pointer(s) to your GraphQL schema. This schema will be the base schema for all your outputs.
323 * You can use one of the following:
324 *
325 * - URL pointing to a GraphQL endpoint
326 * - Path to a local `.json` file
327 * - Path to a local `.graphql` file
328 * - Glob expression pointing to multiple `.graphql` files
329 * - Path to a local code file (for example: `.js`) that exports `GraphQLSchema` object
330 * - Inline string containing GraphQL SDL schema definition
331 *
332 * You can specify either a single schema, or multiple, and GraphQL Code Generator will merge the schemas into a single schema.
333 *
334 * For more details: https://graphql-code-generator.com/docs/config-reference/schema-field
335 */
336 schema?: InstanceOrArray<Schema>;
337 /**
338 * @description A path to a file which defines custom Node.JS require() handlers for custom file extensions.
339 * This is essential if the code generator has to go through files which require other files in an unsupported format (by default).
340 *
341 * For more details: https://graphql-code-generator.com/docs/config-reference/require-field
342 * See more information about require.extensions: https://gist.github.com/jamestalmage/df922691475cff66c7e6.
343 *
344 * Note: values that specified in your .yml file will get loaded after loading the config .yml file.
345 */
346 require?: RequireExtension;
347 /**
348 * @description Name for a library that implements `fetch`.
349 * Use this to tell codegen to use that to fetch schemas in a custom way.
350 */
351 customFetch?: string;
352 /**
353 * @description A pointer(s) to your GraphQL documents: query, mutation, subscription and fragment. These documents will be loaded into for all your output files.
354 * You can use one of the following:
355 *
356 * - Path to a local `.graphql` file
357 * - Path to a code file (for example: `.js` or `.tsx`) containing GraphQL operation strings.
358 * - Glob expression pointing to multiple `.graphql` files
359 * - Glob expression pointing to multiple code files
360 * - Inline string containing GraphQL SDL operation definition
361 *
362 * You can specify either a single file, or multiple.
363 *
364 * For more details: https://graphql-code-generator.com/docs/config-reference/documents-field
365 */
366 documents?: InstanceOrArray<OperationDocument>;
367 /**
368 * @type object
369 * @additionalProperties true
370 * @description Configuration object containing key => value that will be passes to the plugins.
371 * Specifying configuration in this level of your configuration file will pass it to all plugins, in all outputs.
372 *
373 * The options may vary depends on what plugins you are using.
374 *
375 * For more details: https://graphql-code-generator.com/docs/config-reference/config-field
376 */
377 config?: PluginConfig;
378 /**
379 * @description A map where the key represents an output path for the generated code and the value represents a set of options which are relevant for that specific file.
380 *
381 * For more details: https://graphql-code-generator.com/docs/config-reference/codegen-config
382 */
383 generates: {
384 [outputPath: string]: ConfiguredOutput | ConfiguredPlugin[];
385 };
386 /**
387 * @description A flag to overwrite files if they already exist when generating code (`true` by default).
388 *
389 * For more details: https://graphql-code-generator.com/docs/config-reference/codegen-config
390 */
391 overwrite?: boolean;
392 /**
393 * @description A flag to trigger codegen when there are changes in the specified GraphQL schemas.
394 *
395 * You can either specify a boolean to turn it on/off or specify an array of glob patterns to add custom files to the watch.
396 *
397 * For more details: https://graphql-code-generator.com/docs/getting-started/development-workflow#watch-mode
398 */
399 watch?: boolean | string | string[];
400 /**
401 * @description Allows overriding the behavior of watch to use stat polling over native file watching support.
402 *
403 * Config fields have the same defaults and sematics as the identically named ones for chokidar.
404 *
405 * For more details: https://graphql-code-generator.com/docs/getting-started/development-workflow#watch-mode
406 */
407 watchConfig?: {
408 usePolling: boolean;
409 interval?: number;
410 };
411 /**
412 * @description A flag to suppress non-zero exit code when there are no documents to generate.
413 */
414 ignoreNoDocuments?: boolean;
415 /**
416 * @description A flag to disable adding `.js` extension to the output file. Default: `true`.
417 */
418 emitLegacyCommonJSImports?: boolean;
419 /**
420 * @description A flag to suppress printing errors when they occur.
421 */
422 silent?: boolean;
423 /**
424 * @description A flag to output more detailed information about tasks
425 */
426 verbose?: boolean;
427 /**
428 * @description A flag to output debug logs
429 */
430 debug?: boolean;
431 /**
432 * @description A flag to print only errors.
433 */
434 errorsOnly?: boolean;
435 /**
436 * @description If you are using the programmatic API in a browser environment, you can override this configuration to load your plugins in a way different than require.
437 */
438 pluginLoader?: PackageLoaderFn<CodegenPlugin>;
439 /**
440 * @description Additional context passed to plugins
441 */
442 pluginContext?: {
443 [key: string]: any;
444 };
445 /**
446 * @description Allows you to override the configuration for `@graphql-tools/graphql-tag-pluck`, the tool that extracts your GraphQL operations from your code files.
447 *
448 * For more details: https://graphql-code-generator.com/docs/config-reference/documents-field#graphql-tag-pluck
449 */
450 pluckConfig?: {
451 /**
452 * @description An array of package name and identifier that will be used to track down your gql usages and imports. Use this if your code files imports gql from another library or you have a custom gql tag. identifier is the named export, so don't provide it if the tag function is imported as default.
453 */
454 modules?: Array<{
455 /**
456 * @description the name of the NPM package name you wish to look for
457 */
458 name: string;
459 /**
460 * @description the tag identifier name you wish to look for
461 */
462 identifier?: string;
463 }>;
464 /**
465 * @description Configures the magic GraphQL comments to look for. The default is `GraphQL`.
466 */
467 magicComment?: string;
468 /**
469 * @description Overrides the name of the default GraphQL name identifier.
470 */
471 globalIdentifier?: string;
472 };
473 /**
474 * @description Specifies scripts to run when events are happening in the codegen core.
475 * Hooks defined on that level will effect all output files.
476 *
477 * For more details: https://graphql-code-generator.com/docs/config-reference/lifecycle-hooks
478 */
479 hooks?: Partial<LifecycleHooksDefinition>;
480 }
481 type ComplexPluginOutput = {
482 content: string;
483 prepend?: string[];
484 append?: string[];
485 };
486 type PluginOutput = string | ComplexPluginOutput;
487 type HookFunction = (...args: any[]) => void | Promise<void>;
488 /**
489 * @description All available lifecycle hooks
490 * @additionalProperties false
491 */
492 type LifecycleHooksDefinition<T = string | HookFunction | (string | HookFunction)[]> = {
493 /**
494 * @description Triggered with no arguments when the codegen starts (after the `codegen.yml` has beed parsed).
495 *
496 * Specify a shell command to run.
497 */
498 afterStart: T;
499 /**
500 * @description Triggered with no arguments, right before the codegen closes, or when watch mode is stopped.
501 *
502 * Specify a shell command to run.
503 */
504 beforeDone: T;
505 /**
506 * @description Triggered every time a file changes when using watch mode.
507 * Triggered with two arguments: the type of the event (for example, `changed`) and the path of the file.
508 */
509 onWatchTriggered: T;
510 /**
511 * @description Triggered in case of a general error in the codegen. The argument is a string containing the error.
512 */
513 onError: T;
514 /**
515 * @description Triggered after a file is written to the file-system. Executed with the path for the file.
516 * If the content of the file hasn't changed since last execution - this hooks won't be triggered.
517 *
518 * > This is a very useful hook, you can use it for integration with Prettier or other linters.
519 */
520 afterOneFileWrite: T;
521 /**
522 * @description Executed after writing all the files to the file-system.
523 * Triggered with multiple arguments - paths for all files.
524 */
525 afterAllFileWrite: T;
526 /**
527 * @description Triggered before a file is written to the file-system. Executed with the path for the file.
528 *
529 * If the content of the file hasn't changed since last execution - this hooks won't be triggered.
530 */
531 beforeOneFileWrite: T;
532 /**
533 * @description Executed after the codegen has done creating the output and before writing the files to the file-system.
534 *
535 * Triggered with multiple arguments - paths for all relevant files.
536 *
537 * > Not all the files will be actually written to the file-system, because this is triggered before checking if the file has changed since last execution.
538 */
539 beforeAllFileWrite: T;
540 };
541 type SkipDocumentsValidationOptions = {
542 /**
543 * @description Allows you to skip specific rules while validating the documents.
544 * See all the rules; https://github.com/graphql/graphql-js/tree/main/src/validation/rules
545 */
546 ignoreRules?: string[];
547 /**
548 * @description Ignore duplicate documents validation
549 */
550 skipDuplicateValidation?: boolean;
551 /**
552 * @description Skip document validation entirely against the schema
553 */
554 skipValidationAgainstSchema?: boolean;
555 } | boolean;
556}
557export declare function isComplexPluginOutput(obj: Types.PluginOutput): obj is Types.ComplexPluginOutput;
558export declare type PluginFunction<T = any, TOutput extends Types.PluginOutput = Types.PluginOutput> = (schema: GraphQLSchema, documents: Types.DocumentFile[], config: T, info?: {
559 outputFile?: string;
560 allPlugins?: Types.ConfiguredPlugin[];
561 pluginContext?: {
562 [key: string]: any;
563 };
564 [key: string]: any;
565}) => Types.Promisable<TOutput>;
566export declare type PluginValidateFn<T = any> = (schema: GraphQLSchema, documents: Types.DocumentFile[], config: T, outputFile: string, allPlugins: Types.ConfiguredPlugin[], pluginContext?: {
567 [key: string]: any;
568}) => Types.Promisable<void>;
569export declare type AddToSchemaResult = string | DocumentNode | undefined;
570export interface CodegenPlugin<T = any> {
571 plugin: PluginFunction<T>;
572 addToSchema?: AddToSchemaResult | ((config: T) => AddToSchemaResult);
573 validate?: PluginValidateFn;
574}
575
\No newline at end of file