UNPKG

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