buildASTSchema.mjs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 { GraphQLSkipDirective, GraphQLIncludeDirective, GraphQLDeprecatedDirective, GraphQLSpecifiedByDirective } 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. if (!directives.some(function (directive) {
  59. return directive.name === 'skip';
  60. })) {
  61. directives.push(GraphQLSkipDirective);
  62. }
  63. if (!directives.some(function (directive) {
  64. return directive.name === 'include';
  65. })) {
  66. directives.push(GraphQLIncludeDirective);
  67. }
  68. if (!directives.some(function (directive) {
  69. return directive.name === 'deprecated';
  70. })) {
  71. directives.push(GraphQLDeprecatedDirective);
  72. }
  73. if (!directives.some(function (directive) {
  74. return directive.name === 'specifiedBy';
  75. })) {
  76. directives.push(GraphQLSpecifiedByDirective);
  77. }
  78. return new GraphQLSchema(config);
  79. }
  80. /**
  81. * A helper function to build a GraphQLSchema directly from a source
  82. * document.
  83. */
  84. export function buildSchema(source, options) {
  85. var document = parse(source, {
  86. noLocation: options === null || options === void 0 ? void 0 : options.noLocation,
  87. allowLegacySDLEmptyFields: options === null || options === void 0 ? void 0 : options.allowLegacySDLEmptyFields,
  88. allowLegacySDLImplementsInterfaces: options === null || options === void 0 ? void 0 : options.allowLegacySDLImplementsInterfaces,
  89. experimentalFragmentVariables: options === null || options === void 0 ? void 0 : options.experimentalFragmentVariables
  90. });
  91. return buildASTSchema(document, {
  92. commentDescriptions: options === null || options === void 0 ? void 0 : options.commentDescriptions,
  93. assumeValidSDL: options === null || options === void 0 ? void 0 : options.assumeValidSDL,
  94. assumeValid: options === null || options === void 0 ? void 0 : options.assumeValid
  95. });
  96. }