UNPKG

1.89 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.executePlugin = void 0;
4const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers");
5const graphql_1 = require("graphql");
6async 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 // FIXME: Sync validate signature with plugin signature
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}
41exports.executePlugin = executePlugin;