buildASTSchema.mjs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { devAssert } from '../jsutils/devAssert.mjs';
  2. import { Kind } from '../language/kinds.mjs';
  3. import { parse } from '../language/parser.mjs';
  4. import { specifiedDirectives } from '../type/directives.mjs';
  5. import { GraphQLSchema } from '../type/schema.mjs';
  6. import { assertValidSDL } from '../validation/validate.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. * Mutation and Subscription.
  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. export function buildASTSchema(documentAST, options) {
  19. (documentAST != null && documentAST.kind === Kind.DOCUMENT) ||
  20. devAssert(false, 'Must provide valid Document AST.');
  21. if (
  22. (options === null || options === void 0 ? void 0 : options.assumeValid) !==
  23. true &&
  24. (options === null || options === void 0
  25. ? void 0
  26. : options.assumeValidSDL) !== true
  27. ) {
  28. assertValidSDL(documentAST);
  29. }
  30. const emptySchemaConfig = {
  31. description: undefined,
  32. types: [],
  33. directives: [],
  34. extensions: Object.create(null),
  35. extensionASTNodes: [],
  36. assumeValid: false,
  37. };
  38. const config = extendSchemaImpl(emptySchemaConfig, documentAST, options);
  39. if (config.astNode == null) {
  40. for (const type of config.types) {
  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. // @ts-expect-error validated in `validateSchema`
  47. config.query = type;
  48. break;
  49. case 'Mutation':
  50. // @ts-expect-error validated in `validateSchema`
  51. config.mutation = type;
  52. break;
  53. case 'Subscription':
  54. // @ts-expect-error validated in `validateSchema`
  55. config.subscription = type;
  56. break;
  57. }
  58. }
  59. }
  60. const directives = [
  61. ...config.directives, // If specified directives were not explicitly declared, add them.
  62. ...specifiedDirectives.filter((stdDirective) =>
  63. config.directives.every(
  64. (directive) => directive.name !== stdDirective.name,
  65. ),
  66. ),
  67. ];
  68. return new GraphQLSchema({ ...config, directives });
  69. }
  70. /**
  71. * A helper function to build a GraphQLSchema directly from a source
  72. * document.
  73. */
  74. export function buildSchema(source, options) {
  75. const document = parse(source, {
  76. noLocation:
  77. options === null || options === void 0 ? void 0 : options.noLocation,
  78. allowLegacyFragmentVariables:
  79. options === null || options === void 0
  80. ? void 0
  81. : options.allowLegacyFragmentVariables,
  82. });
  83. return buildASTSchema(document, {
  84. assumeValidSDL:
  85. options === null || options === void 0 ? void 0 : options.assumeValidSDL,
  86. assumeValid:
  87. options === null || options === void 0 ? void 0 : options.assumeValid,
  88. });
  89. }