UNPKG

2.27 kBJavaScriptView Raw
1import { GraphQLObjectType, GraphQLInterfaceType, GraphQLUnionType, } from 'graphql';
2import { MapperKind, mapSchema, memoize1 } from '@graphql-tools/utils';
3import { defaultMergedResolver, applySchemaTransforms } from '@graphql-tools/delegate';
4import { generateProxyingResolvers } from './generateProxyingResolvers.js';
5export const wrapSchema = memoize1(function wrapSchema(subschemaConfig) {
6 const targetSchema = subschemaConfig.schema;
7 const proxyingResolvers = generateProxyingResolvers(subschemaConfig);
8 const schema = createWrappingSchema(targetSchema, proxyingResolvers);
9 return applySchemaTransforms(schema, subschemaConfig);
10});
11function createWrappingSchema(schema, proxyingResolvers) {
12 return mapSchema(schema, {
13 [MapperKind.ROOT_OBJECT]: type => {
14 var _a;
15 const config = type.toConfig();
16 const fieldConfigMap = config.fields;
17 for (const fieldName in fieldConfigMap) {
18 const field = fieldConfigMap[fieldName];
19 if (field == null) {
20 continue;
21 }
22 fieldConfigMap[fieldName] = {
23 ...field,
24 ...(_a = proxyingResolvers[type.name]) === null || _a === void 0 ? void 0 : _a[fieldName],
25 };
26 }
27 return new GraphQLObjectType(config);
28 },
29 [MapperKind.OBJECT_TYPE]: type => {
30 const config = type.toConfig();
31 config.isTypeOf = undefined;
32 for (const fieldName in config.fields) {
33 const field = config.fields[fieldName];
34 if (field == null) {
35 continue;
36 }
37 field.resolve = defaultMergedResolver;
38 field.subscribe = undefined;
39 }
40 return new GraphQLObjectType(config);
41 },
42 [MapperKind.INTERFACE_TYPE]: type => {
43 const config = type.toConfig();
44 delete config.resolveType;
45 return new GraphQLInterfaceType(config);
46 },
47 [MapperKind.UNION_TYPE]: type => {
48 const config = type.toConfig();
49 delete config.resolveType;
50 return new GraphQLUnionType(config);
51 },
52 });
53}