UNPKG

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