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