-
-
Notifications
You must be signed in to change notification settings - Fork 816
/
makeExecutableSchema.ts
80 lines (64 loc) · 2.66 KB
/
makeExecutableSchema.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
75
76
77
78
79
80
import { GraphQLFieldResolver } from 'graphql';
import { mergeDeep, SchemaDirectiveVisitor } from '@graphql-tools/utils';
import { addResolversToSchema } from './addResolversToSchema';
import { attachDirectiveResolvers } from './attachDirectiveResolvers';
import { assertResolversPresent } from './assertResolversPresent';
import { addSchemaLevelResolver } from './addSchemaLevelResolver';
import { buildSchemaFromTypeDefinitions } from './buildSchemaFromTypeDefinitions';
import { addErrorLoggingToSchema } from './addErrorLoggingToSchema';
import { addCatchUndefinedToSchema } from './addCatchUndefinedToSchema';
import { IExecutableSchemaDefinition } from './types';
export function makeExecutableSchema<TContext = any>({
typeDefs,
resolvers = {},
logger,
allowUndefinedInResolve = true,
resolverValidationOptions = {},
directiveResolvers,
schemaDirectives,
schemaTransforms = [],
parseOptions = {},
inheritResolversFromInterfaces = false,
}: IExecutableSchemaDefinition<TContext>) {
// Validate and clean up arguments
if (typeof resolverValidationOptions !== 'object') {
throw new Error('Expected `resolverValidationOptions` to be an object');
}
if (!typeDefs) {
throw new Error('Must provide typeDefs');
}
// We allow passing in an array of resolver maps, in which case we merge them
const resolverMap: any = Array.isArray(resolvers) ? resolvers.reduce(mergeDeep, {}) : resolvers;
// Arguments are now validated and cleaned up
let schema = buildSchemaFromTypeDefinitions(typeDefs, parseOptions);
schema = addResolversToSchema({
schema,
resolvers: resolverMap,
resolverValidationOptions,
inheritResolversFromInterfaces,
});
assertResolversPresent(schema, resolverValidationOptions);
if (!allowUndefinedInResolve) {
schema = addCatchUndefinedToSchema(schema);
}
if (logger != null) {
schema = addErrorLoggingToSchema(schema, logger);
}
if (typeof resolvers['__schema'] === 'function') {
// TODO a bit of a hack now, better rewrite generateSchema to attach it there.
// not doing that now, because I'd have to rewrite a lot of tests.
schema = addSchemaLevelResolver(schema, resolvers['__schema'] as GraphQLFieldResolver<any, any>);
}
schemaTransforms.forEach(schemaTransform => {
schema = schemaTransform(schema);
});
// directive resolvers are implemented using SchemaDirectiveVisitor.visitSchemaDirectives
// schema visiting modifies the schema in place
if (directiveResolvers != null) {
schema = attachDirectiveResolvers(schema, directiveResolvers);
}
if (schemaDirectives != null) {
SchemaDirectiveVisitor.visitSchemaDirectives(schema, schemaDirectives);
}
return schema;
}