@graphql-mesh/merger-stitching
Version:
55 lines (54 loc) • 2.15 kB
JavaScript
import { stitchSchemas, ValidationLevel } from '@graphql-tools/stitch';
export default class StitchingMerger {
constructor(options) {
this.name = 'stitching';
this.logger = options.logger;
}
getUnifiedSchema(context) {
const { rawSources, typeDefs, resolvers } = context;
this.logger.debug(`Stitching the source schemas`);
const unifiedSchema = stitchSchemas({
subschemas: rawSources,
typeDefs,
resolvers,
resolverValidationOptions: {
requireResolversForAllFields: 'ignore',
requireResolversForArgs: 'ignore',
requireResolversForNonScalar: 'ignore',
requireResolversForResolveType: 'ignore',
requireResolversToMatchSchema: 'ignore',
},
typeMergingOptions: {
validationSettings: {
validationLevel: ValidationLevel.Off,
},
},
mergeDirectives: true,
});
this.logger.debug(`sourceMap is being generated and attached to the unified schema`);
unifiedSchema.extensions = unifiedSchema.extensions || {};
Object.assign(unifiedSchema.extensions, {
sourceMap: new Proxy({}, {
get: (_, pKey) => {
if (pKey === 'get') {
return (rawSource) => {
const stitchingInfo = unifiedSchema.extensions.stitchingInfo;
for (const [subschemaConfig, subschema] of stitchingInfo.subschemaMap) {
if (subschemaConfig.name === rawSource.name) {
return subschema.transformedSchema;
}
}
return undefined;
};
}
return () => {
throw new Error('Not Implemented');
};
},
}),
});
return {
schema: unifiedSchema,
};
}
}