buildASTSchema.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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"));
  8. var _kinds = require("../language/kinds");
  9. var _parser = require("../language/parser");
  10. var _validate = require("../validation/validate");
  11. var _schema = require("../type/schema");
  12. var _directives = require("../type/directives");
  13. var _extendSchema = require("./extendSchema");
  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. if (!directives.some(function (directive) {
  66. return directive.name === 'skip';
  67. })) {
  68. directives.push(_directives.GraphQLSkipDirective);
  69. }
  70. if (!directives.some(function (directive) {
  71. return directive.name === 'include';
  72. })) {
  73. directives.push(_directives.GraphQLIncludeDirective);
  74. }
  75. if (!directives.some(function (directive) {
  76. return directive.name === 'deprecated';
  77. })) {
  78. directives.push(_directives.GraphQLDeprecatedDirective);
  79. }
  80. if (!directives.some(function (directive) {
  81. return directive.name === 'specifiedBy';
  82. })) {
  83. directives.push(_directives.GraphQLSpecifiedByDirective);
  84. }
  85. return new _schema.GraphQLSchema(config);
  86. }
  87. /**
  88. * A helper function to build a GraphQLSchema directly from a source
  89. * document.
  90. */
  91. function buildSchema(source, options) {
  92. var document = (0, _parser.parse)(source, {
  93. noLocation: options === null || options === void 0 ? void 0 : options.noLocation,
  94. allowLegacySDLEmptyFields: options === null || options === void 0 ? void 0 : options.allowLegacySDLEmptyFields,
  95. allowLegacySDLImplementsInterfaces: options === null || options === void 0 ? void 0 : options.allowLegacySDLImplementsInterfaces,
  96. experimentalFragmentVariables: options === null || options === void 0 ? void 0 : options.experimentalFragmentVariables
  97. });
  98. return buildASTSchema(document, {
  99. commentDescriptions: options === null || options === void 0 ? void 0 : options.commentDescriptions,
  100. assumeValidSDL: options === null || options === void 0 ? void 0 : options.assumeValidSDL,
  101. assumeValid: options === null || options === void 0 ? void 0 : options.assumeValid
  102. });
  103. }