1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.executePlugin = void 0;
|
4 | const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers");
|
5 | const graphql_1 = require("graphql");
|
6 | async function executePlugin(options, plugin) {
|
7 | if (!plugin?.plugin || typeof plugin.plugin !== 'function') {
|
8 | throw new Error(`Invalid Custom Plugin "${options.name}" \n
|
9 | Plugin ${options.name} does not export a valid JS object with "plugin" function.
|
10 |
|
11 | Make sure your custom plugin is written in the following form:
|
12 |
|
13 | module.exports = {
|
14 | plugin: (schema, documents, config) => {
|
15 | return 'my-custom-plugin-content';
|
16 | },
|
17 | };
|
18 | `);
|
19 | }
|
20 | const outputSchema = options.schemaAst || (0, graphql_1.buildASTSchema)(options.schema, options.config);
|
21 | const documents = options.documents || [];
|
22 | const pluginContext = options.pluginContext || {};
|
23 | const profiler = options.profiler ?? (0, plugin_helpers_1.createNoopProfiler)();
|
24 | if (plugin.validate && typeof plugin.validate === 'function') {
|
25 | try {
|
26 |
|
27 | await profiler.run(async () => plugin.validate(outputSchema, documents, options.config, options.outputFilename, options.allPlugins, pluginContext), `Plugin ${options.name} validate`);
|
28 | }
|
29 | catch (e) {
|
30 | throw new Error(`Plugin "${options.name}" validation failed: \n
|
31 | ${e.message}
|
32 | `);
|
33 | }
|
34 | }
|
35 | return profiler.run(() => Promise.resolve(plugin.plugin(outputSchema, documents, typeof options.config === 'object' ? { ...options.config } : options.config, {
|
36 | outputFile: options.outputFilename,
|
37 | allPlugins: options.allPlugins,
|
38 | pluginContext,
|
39 | })), `Plugin ${options.name} execution`);
|
40 | }
|
41 | exports.executePlugin = executePlugin;
|