UNPKG

1.75 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.transformDocuments = void 0;
4const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers");
5async function transformDocuments(options) {
6 var _a;
7 const documentTransforms = options.documentTransforms || [];
8 let documents = options.documents;
9 if (documentTransforms.length === 0 || options.documents.length === 0) {
10 return documents;
11 }
12 const profiler = (_a = options.profiler) !== null && _a !== void 0 ? _a : (0, plugin_helpers_1.createNoopProfiler)();
13 for (const documentTransform of documentTransforms) {
14 const config = typeof documentTransform.config === 'object'
15 ? {
16 ...options.config,
17 ...documentTransform.config,
18 }
19 : {};
20 const { transform } = documentTransform.transformObject;
21 if (transform && typeof transform === 'function') {
22 const name = documentTransform.name;
23 try {
24 await profiler.run(async () => {
25 documents = await transform({
26 documents,
27 schema: options.schema,
28 config,
29 pluginContext: options.pluginContext,
30 });
31 }, `DocumentTransform "${name}" execution`);
32 }
33 catch (e) {
34 throw new Error(`DocumentTransform "${name}" failed: \n
35 ${e.message}
36 `);
37 }
38 }
39 else {
40 throw new Error(`Missing 'transform' function in "${documentTransform.name}"`);
41 }
42 }
43 return documents;
44}
45exports.transformDocuments = transformDocuments;