UNPKG

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