UNPKG

1.95 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 var _a;
8 if (!plugin || !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 // FIXME: Sync validate signature with plugin signature
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}
42exports.executePlugin = executePlugin;