buildASTSchema.mjs 3.5 KB

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