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