makeExecutableSchema.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.makeExecutableSchema = void 0;
  4. const graphql_1 = require("graphql");
  5. const merge_1 = require("@graphql-tools/merge");
  6. const utils_1 = require("@graphql-tools/utils");
  7. const addResolversToSchema_js_1 = require("./addResolversToSchema.js");
  8. const assertResolversPresent_js_1 = require("./assertResolversPresent.js");
  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. */
  52. function makeExecutableSchema({ typeDefs, resolvers = {}, resolverValidationOptions = {}, inheritResolversFromInterfaces = false, updateResolversInPlace = false, schemaExtensions, defaultFieldResolver, ...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?.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. defaultFieldResolver,
  83. });
  84. if (Object.keys(resolverValidationOptions).length > 0) {
  85. (0, assertResolversPresent_js_1.assertResolversPresent)(schema, resolverValidationOptions);
  86. }
  87. if (schemaExtensions) {
  88. schemaExtensions = (0, merge_1.mergeExtensions)((0, utils_1.asArray)(schemaExtensions));
  89. (0, merge_1.applyExtensions)(schema, schemaExtensions);
  90. }
  91. return schema;
  92. }
  93. exports.makeExecutableSchema = makeExecutableSchema;