UNPKG

3.39 kBJavaScriptView Raw
1import { buildASTSchema, buildSchema, isSchema } from 'graphql';
2import { asArray, pruneSchema } from '@graphql-tools/utils';
3import { addResolversToSchema } from './addResolversToSchema.js';
4import { assertResolversPresent } from './assertResolversPresent.js';
5import { applyExtensions, mergeExtensions, mergeResolvers, mergeTypeDefs } from '@graphql-tools/merge';
6/**
7 * Builds a schema from the provided type definitions and resolvers.
8 *
9 * The type definitions are written using Schema Definition Language (SDL). They
10 * can be provided as a string, a `DocumentNode`, a function, or an array of any
11 * of these. If a function is provided, it will be passed no arguments and
12 * should return an array of strings or `DocumentNode`s.
13 *
14 * Note: You can use `graphql-tag` to not only parse a string into a
15 * `DocumentNode` but also to provide additional syntax highlighting in your
16 * editor (with the appropriate editor plugin).
17 *
18 * ```js
19 * const typeDefs = gql`
20 * type Query {
21 * posts: [Post]
22 * author(id: Int!): Author
23 * }
24 * `;
25 * ```
26 *
27 * The `resolvers` object should be a map of type names to nested object, which
28 * themselves map the type's fields to their appropriate resolvers.
29 * See the [Resolvers](/docs/resolvers) section of the documentation for more details.
30 *
31 * ```js
32 * const resolvers = {
33 * Query: {
34 * posts: (obj, args, ctx, info) => getAllPosts(),
35 * author: (obj, args, ctx, info) => getAuthorById(args.id)
36 * }
37 * };
38 * ```
39 *
40 * Once you've defined both the `typeDefs` and `resolvers`, you can create your
41 * schema:
42 *
43 * ```js
44 * const schema = makeExecutableSchema({
45 * typeDefs,
46 * resolvers,
47 * })
48 * ```
49 */
50export function makeExecutableSchema({ typeDefs, resolvers = {}, resolverValidationOptions = {}, parseOptions = {}, inheritResolversFromInterfaces = false, pruningOptions, updateResolversInPlace = false, schemaExtensions, }) {
51 // Validate and clean up arguments
52 if (typeof resolverValidationOptions !== 'object') {
53 throw new Error('Expected `resolverValidationOptions` to be an object');
54 }
55 if (!typeDefs) {
56 throw new Error('Must provide typeDefs');
57 }
58 let schema;
59 if (isSchema(typeDefs)) {
60 schema = typeDefs;
61 }
62 else if (parseOptions === null || parseOptions === void 0 ? void 0 : parseOptions.commentDescriptions) {
63 const mergedTypeDefs = mergeTypeDefs(typeDefs, {
64 ...parseOptions,
65 commentDescriptions: true,
66 });
67 schema = buildSchema(mergedTypeDefs, parseOptions);
68 }
69 else {
70 const mergedTypeDefs = mergeTypeDefs(typeDefs, parseOptions);
71 schema = buildASTSchema(mergedTypeDefs, parseOptions);
72 }
73 if (pruningOptions) {
74 schema = pruneSchema(schema);
75 }
76 // We allow passing in an array of resolver maps, in which case we merge them
77 schema = addResolversToSchema({
78 schema,
79 resolvers: mergeResolvers(resolvers),
80 resolverValidationOptions,
81 inheritResolversFromInterfaces,
82 updateResolversInPlace,
83 });
84 if (Object.keys(resolverValidationOptions).length > 0) {
85 assertResolversPresent(schema, resolverValidationOptions);
86 }
87 if (schemaExtensions) {
88 schemaExtensions = mergeExtensions(asArray(schemaExtensions));
89 applyExtensions(schema, schemaExtensions);
90 }
91 return schema;
92}