UNPKG

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