buildASTSchema.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.buildASTSchema = buildASTSchema;
  6. exports.buildSchema = buildSchema;
  7. var _devAssert = _interopRequireDefault(require("../jsutils/devAssert.js"));
  8. var _kinds = require("../language/kinds.js");
  9. var _parser = require("../language/parser.js");
  10. var _validate = require("../validation/validate.js");
  11. var _schema = require("../type/schema.js");
  12. var _directives = require("../type/directives.js");
  13. var _extendSchema = require("./extendSchema.js");
  14. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  15. /**
  16. * This takes the ast of a schema document produced by the parse function in
  17. * src/language/parser.js.
  18. *
  19. * If no schema definition is provided, then it will look for types named Query
  20. * and Mutation.
  21. *
  22. * Given that AST it constructs a GraphQLSchema. The resulting schema
  23. * has no resolve methods, so execution will use default resolvers.
  24. *
  25. * Accepts options as a second argument:
  26. *
  27. * - commentDescriptions:
  28. * Provide true to use preceding comments as the description.
  29. *
  30. */
  31. function buildASTSchema(documentAST, options) {
  32. documentAST != null && documentAST.kind === _kinds.Kind.DOCUMENT || (0, _devAssert.default)(0, 'Must provide valid Document AST.');
  33. if ((options === null || options === void 0 ? void 0 : options.assumeValid) !== true && (options === null || options === void 0 ? void 0 : options.assumeValidSDL) !== true) {
  34. (0, _validate.assertValidSDL)(documentAST);
  35. }
  36. var emptySchemaConfig = {
  37. description: undefined,
  38. types: [],
  39. directives: [],
  40. extensions: undefined,
  41. extensionASTNodes: [],
  42. assumeValid: false
  43. };
  44. var config = (0, _extendSchema.extendSchemaImpl)(emptySchemaConfig, documentAST, options);
  45. if (config.astNode == null) {
  46. for (var _i2 = 0, _config$types2 = config.types; _i2 < _config$types2.length; _i2++) {
  47. var type = _config$types2[_i2];
  48. switch (type.name) {
  49. // Note: While this could make early assertions to get the correctly
  50. // typed values below, that would throw immediately while type system
  51. // validation with validateSchema() will produce more actionable results.
  52. case 'Query':
  53. config.query = type;
  54. break;
  55. case 'Mutation':
  56. config.mutation = type;
  57. break;
  58. case 'Subscription':
  59. config.subscription = type;
  60. break;
  61. }
  62. }
  63. }
  64. var directives = config.directives; // If specified directives were not explicitly declared, add them.
  65. var _loop = function _loop(_i4) {
  66. var stdDirective = _directives.specifiedDirectives[_i4];
  67. if (directives.every(function (directive) {
  68. return directive.name !== stdDirective.name;
  69. })) {
  70. directives.push(stdDirective);
  71. }
  72. };
  73. for (var _i4 = 0; _i4 < _directives.specifiedDirectives.length; _i4++) {
  74. _loop(_i4);
  75. }
  76. return new _schema.GraphQLSchema(config);
  77. }
  78. /**
  79. * A helper function to build a GraphQLSchema directly from a source
  80. * document.
  81. */
  82. function buildSchema(source, options) {
  83. var document = (0, _parser.parse)(source, {
  84. noLocation: options === null || options === void 0 ? void 0 : options.noLocation,
  85. allowLegacySDLEmptyFields: options === null || options === void 0 ? void 0 : options.allowLegacySDLEmptyFields,
  86. allowLegacySDLImplementsInterfaces: options === null || options === void 0 ? void 0 : options.allowLegacySDLImplementsInterfaces,
  87. experimentalFragmentVariables: options === null || options === void 0 ? void 0 : options.experimentalFragmentVariables
  88. });
  89. return buildASTSchema(document, {
  90. commentDescriptions: options === null || options === void 0 ? void 0 : options.commentDescriptions,
  91. assumeValidSDL: options === null || options === void 0 ? void 0 : options.assumeValidSDL,
  92. assumeValid: options === null || options === void 0 ? void 0 : options.assumeValid
  93. });
  94. }