1 | 'use strict';
|
2 |
|
3 | const stitch = require('@graphql-tools/stitch');
|
4 | const wrap = require('@graphql-tools/wrap');
|
5 | const graphql = require('graphql');
|
6 | const schema = require('@graphql-tools/schema');
|
7 | const utils = require('@graphql-mesh/utils');
|
8 |
|
9 | const mergeSingleSchema = ({ rawSources, typeDefs, resolvers, transforms }) => {
|
10 | if (rawSources.length !== 1) {
|
11 | throw new Error('This merger supports only one schema');
|
12 | }
|
13 | const [source] = rawSources;
|
14 | let schema$1 = source.schema;
|
15 | let wrapTransforms = [];
|
16 | let noWrapTransforms = [];
|
17 | if (transforms === null || transforms === void 0 ? void 0 : transforms.length) {
|
18 | const transformGroups = utils.groupTransforms(transforms);
|
19 | wrapTransforms = transformGroups.wrapTransforms;
|
20 | noWrapTransforms = transformGroups.noWrapTransforms;
|
21 | }
|
22 | if (source.executor || source.subscriber || source.transforms.length) {
|
23 | const firstRoundTransforms = [...source.transforms];
|
24 | if (!typeDefs && !resolvers) {
|
25 | firstRoundTransforms.push(...wrapTransforms, ...noWrapTransforms);
|
26 | }
|
27 | schema$1 = wrap.wrapSchema({
|
28 | ...source,
|
29 | schema: schema$1,
|
30 | transforms: firstRoundTransforms,
|
31 | });
|
32 | }
|
33 | if (typeDefs || resolvers) {
|
34 | typeDefs === null || typeDefs === void 0 ? void 0 : typeDefs.forEach(typeDef => {
|
35 | schema$1 = graphql.extendSchema(schema$1, typeDef);
|
36 | });
|
37 | if (resolvers) {
|
38 | schema$1 = schema.addResolversToSchema({
|
39 | schema: schema$1,
|
40 | resolvers,
|
41 | updateResolversInPlace: true,
|
42 | });
|
43 | }
|
44 | if (wrapTransforms.length) {
|
45 | schema$1 = wrap.wrapSchema({
|
46 | schema: schema$1,
|
47 | transforms: [...wrapTransforms, ...noWrapTransforms],
|
48 | });
|
49 | }
|
50 | else if (noWrapTransforms.length) {
|
51 | schema$1 = utils.applySchemaTransforms(schema$1, undefined, schema$1, noWrapTransforms);
|
52 | }
|
53 | }
|
54 | schema$1.extensions = schema$1.extensions || {};
|
55 | Object.defineProperty(schema$1.extensions, 'sourceMap', {
|
56 | get: () => new Map([[source, schema$1]]),
|
57 | });
|
58 | return schema$1;
|
59 | };
|
60 |
|
61 | const mergeUsingStitching = async function (options) {
|
62 | if (options.rawSources.length === 1) {
|
63 | return mergeSingleSchema(options);
|
64 | }
|
65 | const { rawSources, typeDefs, resolvers, transforms } = options;
|
66 | let unifiedSchema = stitch.stitchSchemas({
|
67 | subschemas: rawSources,
|
68 | typeDefs,
|
69 | resolvers,
|
70 | });
|
71 | unifiedSchema.extensions = unifiedSchema.extensions || {};
|
72 | Object.defineProperty(unifiedSchema.extensions, 'sourceMap', {
|
73 | get: () => {
|
74 | const stitchingInfo = unifiedSchema.extensions.stitchingInfo;
|
75 | const entries = stitchingInfo.subschemaMap.entries();
|
76 | return new Map([...entries].map(([subschemaConfig, subschema]) => [subschemaConfig, subschema.transformedSchema]));
|
77 | },
|
78 | });
|
79 | if (transforms === null || transforms === void 0 ? void 0 : transforms.length) {
|
80 | const { noWrapTransforms, wrapTransforms } = utils.groupTransforms(transforms);
|
81 | if (wrapTransforms.length) {
|
82 | unifiedSchema = wrap.wrapSchema({
|
83 | schema: unifiedSchema,
|
84 | batch: true,
|
85 | transforms: wrapTransforms,
|
86 | });
|
87 | }
|
88 | if (noWrapTransforms.length) {
|
89 | unifiedSchema = utils.applySchemaTransforms(unifiedSchema, { schema: unifiedSchema }, null, noWrapTransforms);
|
90 | }
|
91 | }
|
92 | return unifiedSchema;
|
93 | };
|
94 |
|
95 | module.exports = mergeUsingStitching;
|
96 |
|