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