-
-
Notifications
You must be signed in to change notification settings - Fork 796
/
makeRemoteExecutableSchema.ts
74 lines (66 loc) · 1.97 KB
/
makeRemoteExecutableSchema.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { ApolloLink } from 'apollo-link';
import {
GraphQLFieldResolver,
GraphQLSchema,
BuildSchemaOptions,
} from 'graphql';
import { Fetcher } from '../Interfaces';
import { buildSchema } from '../polyfills/index';
import linkToFetcher from '../stitch/linkToFetcher';
import { delegateToSchema } from '../delegate';
import { wrapSchema } from './wrapSchema';
export default function makeRemoteExecutableSchema({
schema: schemaOrTypeDefs,
link,
fetcher,
createResolver = defaultCreateRemoteResolver,
createSubscriptionResolver = defaultCreateRemoteSubscriptionResolver,
buildSchemaOptions,
}: {
schema: GraphQLSchema | string;
link?: ApolloLink;
fetcher?: Fetcher;
createResolver?: (fetcher: Fetcher) => GraphQLFieldResolver<any, any>;
createSubscriptionResolver?: (
link: ApolloLink,
) => GraphQLFieldResolver<any, any>;
buildSchemaOptions?: BuildSchemaOptions;
}): GraphQLSchema {
let finalFetcher: Fetcher = fetcher;
if (finalFetcher == null && link != null) {
finalFetcher = linkToFetcher(link);
}
const targetSchema =
typeof schemaOrTypeDefs === 'string'
? buildSchema(schemaOrTypeDefs, buildSchemaOptions)
: schemaOrTypeDefs;
return wrapSchema({
schema: targetSchema,
createProxyingResolver: (_schema, _transforms, operation) => {
if (operation === 'query' || operation === 'mutation') {
return createResolver(finalFetcher);
}
return createSubscriptionResolver(link);
},
});
}
export function defaultCreateRemoteResolver(
fetcher: Fetcher,
): GraphQLFieldResolver<any, any> {
return (_parent, _args, context, info) =>
delegateToSchema({
schema: { schema: info.schema, fetcher },
context,
info,
});
}
export function defaultCreateRemoteSubscriptionResolver(
link: ApolloLink,
): GraphQLFieldResolver<any, any> {
return (_parent, _args, context, info) =>
delegateToSchema({
schema: { schema: info.schema, link },
context,
info,
});
}