buildASTSchema.js 3.3 KB

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