UNPKG

19.6 kBTypeScriptView Raw
1import { GraphQLSchema, DocumentNode } from 'graphql';
2import { Source } from '@graphql-tools/utils';
3export declare namespace Types {
4 interface GenerateOptions {
5 filename: string;
6 plugins: Types.ConfiguredPlugin[];
7 schema: DocumentNode;
8 schemaAst?: GraphQLSchema;
9 documents: Types.DocumentFile[];
10 config: {
11 [key: string]: any;
12 };
13 pluginMap: {
14 [name: string]: CodegenPlugin;
15 };
16 skipDocumentsValidation?: boolean;
17 }
18 type FileOutput = {
19 filename: string;
20 content: string;
21 hooks?: {
22 beforeOneFileWrite?: LifecycleHooksDefinition['beforeOneFileWrite'];
23 afterOneFileWrite?: LifecycleHooksDefinition['afterOneFileWrite'];
24 };
25 };
26 type DocumentFile = Source;
27 type Promisable<T> = T | Promise<T>;
28 type InstanceOrArray<T> = T | T[];
29 /**
30 * @additionalProperties false
31 * @description Loads schema using a pointer, with a custom loader (code file).
32 */
33 interface SchemaWithLoaderOptions {
34 /**
35 * @description Specify a path to a custom code file (local or module) that will handle the schema loading.
36 */
37 loader: string;
38 }
39 interface SchemaWithLoader {
40 [pointer: string]: SchemaWithLoaderOptions;
41 }
42 /**
43 * @additionalProperties false
44 * @description Loads schema using a pointer, without using `require` while looking for schemas in code files.
45 */
46 interface SchemaFromCodeFileOptions {
47 /**
48 * @description Set this to `true` in order to tell codegen not to try to `require` files in order to find schema/docs
49 */
50 noRequire?: boolean;
51 /**
52 * @description Set this to `true` in order to tell codegen not to try to extract GraphQL AST strings schema/docs
53 */
54 noPluck?: boolean;
55 /**
56 * @description Set this to `true` in order to tell codegen to skip documents validation.
57 */
58 assumeValid?: boolean;
59 }
60 interface SchemaFromCodeFile {
61 [path: string]: SchemaFromCodeFileOptions;
62 }
63 /**
64 * @additionalProperties false
65 * @description Loads a schema from remote endpoint, with custom http options.
66 */
67 interface UrlSchemaOptions {
68 /**
69 * @description HTTP headers you wish to add to the HTTP request sent by codegen to fetch your GraphQL remote schema.
70 */
71 headers?: {
72 [headerName: string]: string;
73 };
74 /**
75 * @description Specify a Node module name, or a custom file, to be used instead of standard `fetch`
76 */
77 customFetch?: string;
78 /**
79 * @description HTTP Method to use, either POST (default) or GET.
80 */
81 method?: string;
82 }
83 interface UrlSchemaWithOptions {
84 [url: string]: UrlSchemaOptions;
85 }
86 type LocalSchemaPath = string;
87 type SchemaGlobPath = string;
88 /**
89 * @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).
90 */
91 type Schema = string | UrlSchemaWithOptions | LocalSchemaPath | SchemaGlobPath | SchemaWithLoader | SchemaFromCodeFile;
92 type OperationDocumentGlobPath = string;
93 /**
94 * @additionalProperties false
95 * @description Specify a path to a custom loader for your GraphQL documents.
96 */
97 interface CustomDocumentLoaderOptions {
98 /**
99 * @description Specify a path to a custom code file (local or module) that will handle the documents loading.
100 */
101 loader: string;
102 }
103 interface CustomDocumentLoader {
104 [path: string]: CustomDocumentLoaderOptions;
105 }
106 type OperationDocument = OperationDocumentGlobPath | CustomDocumentLoader;
107 type PluginConfig<T = any> = {
108 [key: string]: T;
109 };
110 interface ConfiguredPlugin {
111 [name: string]: PluginConfig;
112 }
113 type NamedPlugin = string;
114 type NamedPreset = string;
115 type OutputConfig = NamedPlugin | ConfiguredPlugin;
116 /**
117 * @additionalProperties false
118 */
119 interface ConfiguredOutput {
120 /**
121 * @type array
122 * @items { "$ref": "#/definitions/GeneratedPluginsMap" }
123 * @description List of plugins to apply to this current output file.
124 *
125 * 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.
126 *
127 * You can find a list of available plugins here: https://graphql-code-generator.com/docs/plugins/index
128 * Need a custom plugin? read this: https://graphql-code-generator.com/docs/custom-codegen/index
129 */
130 plugins: OutputConfig[];
131 /**
132 * @description If your setup uses Preset to have a more dynamic setup and output, set the name of your preset here.
133 *
134 * Presets are a way to have more than one file output, for example: https://graphql-code-generator.com/docs/presets/near-operation-file
135 *
136 * 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.
137 *
138 * List of available presets: https://graphql-code-generator.com/docs/presets/presets-index
139 */
140 preset?: string | OutputPreset;
141 /**
142 * @description If your setup uses Preset to have a more dynamic setup and output, set the configuration object of your preset here.
143 *
144 * List of available presets: https://graphql-code-generator.com/docs/presets/presets-index
145 */
146 presetConfig?: {
147 [key: string]: any;
148 };
149 /**
150 * @description A flag to overwrite files if they already exist when generating code (`true` by default).
151 *
152 * For more details: https://graphql-code-generator.com/docs/getting-started/codegen-config
153 */
154 overwrite?: boolean;
155 /**
156 * @description A pointer(s) to your GraphQL documents: query, mutation, subscription and fragment. These documents will be loaded into for all your output files.
157 * You can use one of the following:
158 *
159 * - Path to a local `.graphql` file
160 * - Path to a code file (for example: `.js` or `.tsx`) containing GraphQL operation strings.
161 * - Glob expression pointing to multiple `.graphql` files
162 * - Glob expression pointing to multiple code files
163 * - Inline string containing GraphQL SDL operation definition
164 *
165 * You can specify either a single file, or multiple.
166 *
167 * For more details: https://graphql-code-generator.com/docs/getting-started/documents-field
168 */
169 documents?: InstanceOrArray<OperationDocument>;
170 /**
171 * @description A pointer(s) to your GraphQL schema. This schema will be available only for this specific `generates` record.
172 * You can use one of the following:
173 *
174 * - URL pointing to a GraphQL endpoint
175 * - Path to a local `.json` file
176 * - Path to a local `.graphql` file
177 * - Glob expression pointing to multiple `.graphql` files
178 * - Path to a local code file (for example: `.js`) that exports `GraphQLSchema` object
179 * - Inline string containing GraphQL SDL schema definition
180 *
181 * You can specify either a single schema, or multiple, and GraphQL Code Generator will merge the schemas into a single schema.
182 *
183 * For more details: https://graphql-code-generator.com/docs/getting-started/schema-field
184 */
185 schema?: InstanceOrArray<Schema>;
186 /**
187 * @description Configuration object containing key => value that will be passes to the plugins.
188 * Specifying configuration in this level of your configuration file will pass it to all plugins, in all outputs.
189 *
190 * The options may vary depends on what plugins you are using.
191 *
192 * For more details: https://graphql-code-generator.com/docs/getting-started/config-field
193 */
194 config?: PluginConfig;
195 /**
196 * @description Specifies scripts to run when events are happening in the codegen core.
197 * Hooks defined on that level will effect only the current output files.
198 *
199 * For more details: https://graphql-code-generator.com/docs/getting-started/lifecycle-hooks
200 */
201 hooks?: LifecycleHooksDefinition;
202 }
203 type PresetFnArgs<Config = any, PluginConfig = {
204 [key: string]: any;
205 }> = {
206 presetConfig: Config;
207 baseOutputDir: string;
208 plugins: Types.ConfiguredPlugin[];
209 schema: DocumentNode;
210 schemaAst?: GraphQLSchema;
211 documents: Types.DocumentFile[];
212 config: PluginConfig;
213 pluginMap: {
214 [name: string]: CodegenPlugin;
215 };
216 };
217 type OutputPreset<TPresetConfig = any> = {
218 buildGeneratesSection: (options: PresetFnArgs<TPresetConfig>) => Promisable<GenerateOptions[]>;
219 };
220 type RequireExtension = InstanceOrArray<string>;
221 type PackageLoaderFn<TExpectedResult> = (name: string) => Promisable<TExpectedResult>;
222 /**
223 * @description Represents the root YAML schema for the config file.
224 * @additionalProperties false
225 */
226 interface Config {
227 /**
228 * @description A pointer(s) to your GraphQL schema. This schema will be the base schema for all your outputs.
229 * You can use one of the following:
230 *
231 * - URL pointing to a GraphQL endpoint
232 * - Path to a local `.json` file
233 * - Path to a local `.graphql` file
234 * - Glob expression pointing to multiple `.graphql` files
235 * - Path to a local code file (for example: `.js`) that exports `GraphQLSchema` object
236 * - Inline string containing GraphQL SDL schema definition
237 *
238 * You can specify either a single schema, or multiple, and GraphQL Code Generator will merge the schemas into a single schema.
239 *
240 * For more details: https://graphql-code-generator.com/docs/getting-started/schema-field
241 */
242 schema?: InstanceOrArray<Schema>;
243 /**
244 * @description A path to a file which defines custom Node.JS require() handlers for custom file extensions.
245 * This is essential if the code generator has to go through files which require other files in an unsupported format (by default).
246 *
247 * For more details: https://graphql-code-generator.com/docs/getting-started/require-field
248 * See more information about require.extensions: https://gist.github.com/jamestalmage/df922691475cff66c7e6.
249 *
250 * Note: values that specified in your .yml file will get loaded after loading the config .yml file.
251 */
252 require?: RequireExtension;
253 /**
254 * @description Name for a library that implements `fetch`.
255 * Use this to tell codegen to use that to fetch schemas in a custom way.
256 */
257 customFetch?: string;
258 /**
259 * @description A pointer(s) to your GraphQL documents: query, mutation, subscription and fragment. These documents will be loaded into for all your output files.
260 * You can use one of the following:
261 *
262 * - Path to a local `.graphql` file
263 * - Path to a code file (for example: `.js` or `.tsx`) containing GraphQL operation strings.
264 * - Glob expression pointing to multiple `.graphql` files
265 * - Glob expression pointing to multiple code files
266 * - Inline string containing GraphQL SDL operation definition
267 *
268 * You can specify either a single file, or multiple.
269 *
270 * For more details: https://graphql-code-generator.com/docs/getting-started/documents-field
271 */
272 documents?: InstanceOrArray<OperationDocument>;
273 /**
274 * @type object
275 * @additionalProperties true
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/getting-started/config-field
282 */
283 config?: PluginConfig;
284 /**
285 * @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.
286 *
287 * For more details: https://graphql-code-generator.com/docs/getting-started/codegen-config
288 */
289 generates: {
290 [outputPath: string]: ConfiguredOutput;
291 };
292 /**
293 * @description A flag to overwrite files if they already exist when generating code (`true` by default).
294 *
295 * For more details: https://graphql-code-generator.com/docs/getting-started/codegen-config
296 */
297 overwrite?: boolean;
298 /**
299 * @description A flag to trigger codegen when there are changes in the specified GraphQL schemas.
300 *
301 * 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.
302 *
303 * For more details: https://graphql-code-generator.com/docs/getting-started/development-workflow#watch-mode
304 */
305 watch?: boolean | string | string[];
306 /**
307 * @description A flag to suppress printing errors when they occur.
308 */
309 silent?: boolean;
310 /**
311 * @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.
312 */
313 pluginLoader?: PackageLoaderFn<CodegenPlugin>;
314 /**
315 * @description Allows you to override the configuration for `@graphql-tools/graphql-tag-pluck`, the tool that extracts your GraphQL operations from your code files.
316 *
317 * For more details: https://graphql-code-generator.com/docs/getting-started/documents-field#graphql-tag-pluck
318 */
319 pluckConfig?: {
320 /**
321 * @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.
322 */
323 modules?: Array<{
324 /**
325 * @description the name of the NPM package name you wish to look for
326 */
327 name: string;
328 /**
329 * @description the tag identifier name you wish to look for
330 */
331 identifier?: string;
332 }>;
333 /**
334 * @description Configures the magic GraphQL comments to look for. The default is `GraphQL`.
335 */
336 magicComment?: string;
337 /**
338 * @description Overrides the name of the default GraphQL name identifier.
339 */
340 globalIdentifier?: string;
341 };
342 /**
343 * @description Specifies scripts to run when events are happening in the codegen core.
344 * Hooks defined on that level will effect all output files.
345 *
346 * For more details: https://graphql-code-generator.com/docs/getting-started/lifecycle-hooks
347 */
348 hooks?: LifecycleHooksDefinition;
349 }
350 type ComplexPluginOutput = {
351 content: string;
352 prepend?: string[];
353 append?: string[];
354 };
355 type PluginOutput = string | ComplexPluginOutput;
356 type HookFunction = (...args: any[]) => void | Promise<void>;
357 /**
358 * @description All available lifecycle hooks
359 * @additionalProperties false
360 */
361 type LifecycleHooksDefinition<T = string | HookFunction | (string | HookFunction)[]> = {
362 /**
363 * @description Triggered with no arguments when the codegen starts (after the `codegen.yml` has beed parsed).
364 *
365 * Specify a shell command to run.
366 */
367 afterStart: T;
368 /**
369 * @description Triggered with no arguments, right before the codegen closes, or when watch mode is stopped.
370 *
371 * Specify a shell command to run.
372 */
373 beforeDone: T;
374 /**
375 * @description Triggered every time a file changes when using watch mode.
376 * Triggered with two arguments: the type of the event (for example, `changed`) and the path of the file.
377 */
378 onWatchTriggered: T;
379 /**
380 * @description Triggered in case of a general error in the codegen. The argument is a string containing the error.
381 */
382 onError: T;
383 /**
384 * @description Triggered after a file is written to the file-system. Executed with the path for the file.
385 * If the content of the file hasn't changed since last execution - this hooks won't be triggered.
386 *
387 * > This is a very useful hook, you can use it for integration with Prettier or other linters.
388 */
389 afterOneFileWrite: T;
390 /**
391 * @description Executed after writing all the files to the file-system.
392 * Triggered with multiple arguments - paths for all files.
393 */
394 afterAllFileWrite: T;
395 /**
396 * @description Triggered before a file is written to the file-system. Executed with the path for the file.
397 *
398 * If the content of the file hasn't changed since last execution - this hooks won't be triggered.
399 */
400 beforeOneFileWrite: T;
401 /**
402 * @description Executed after the codegen has done creating the output and before writing the files to the file-system.
403 *
404 * Triggered with multiple arguments - paths for all relevant files.
405 *
406 * > 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.
407 */
408 beforeAllFileWrite: T;
409 };
410}
411export declare function isComplexPluginOutput(obj: Types.PluginOutput): obj is Types.ComplexPluginOutput;
412export declare type PluginFunction<T = any, TOutput extends Types.PluginOutput = Types.PluginOutput> = (schema: GraphQLSchema, documents: Types.DocumentFile[], config: T, info?: {
413 outputFile?: string;
414 allPlugins?: Types.ConfiguredPlugin[];
415 [key: string]: any;
416}) => Types.Promisable<TOutput>;
417export declare type PluginValidateFn<T = any> = (schema: GraphQLSchema, documents: Types.DocumentFile[], config: T, outputFile: string, allPlugins: Types.ConfiguredPlugin[]) => Types.Promisable<void>;
418export declare type AddToSchemaResult = string | DocumentNode | undefined;
419export interface CodegenPlugin<T = any> {
420 plugin: PluginFunction<T>;
421 addToSchema?: AddToSchemaResult | ((config: T) => AddToSchemaResult);
422 validate?: PluginValidateFn;
423}
424
\No newline at end of file