LoneSchemaDefinitionRule.mjs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { GraphQLError } from '../../error/GraphQLError.mjs';
  2. /**
  3. * Lone Schema definition
  4. *
  5. * A GraphQL document is only valid if it contains only one schema definition.
  6. */
  7. export function LoneSchemaDefinitionRule(context) {
  8. var _ref, _ref2, _oldSchema$astNode;
  9. const oldSchema = context.getSchema();
  10. const alreadyDefined =
  11. (_ref =
  12. (_ref2 =
  13. (_oldSchema$astNode =
  14. oldSchema === null || oldSchema === void 0
  15. ? void 0
  16. : oldSchema.astNode) !== null && _oldSchema$astNode !== void 0
  17. ? _oldSchema$astNode
  18. : oldSchema === null || oldSchema === void 0
  19. ? void 0
  20. : oldSchema.getQueryType()) !== null && _ref2 !== void 0
  21. ? _ref2
  22. : oldSchema === null || oldSchema === void 0
  23. ? void 0
  24. : oldSchema.getMutationType()) !== null && _ref !== void 0
  25. ? _ref
  26. : oldSchema === null || oldSchema === void 0
  27. ? void 0
  28. : oldSchema.getSubscriptionType();
  29. let schemaDefinitionsCount = 0;
  30. return {
  31. SchemaDefinition(node) {
  32. if (alreadyDefined) {
  33. context.reportError(
  34. new GraphQLError(
  35. 'Cannot define a new schema within a schema extension.',
  36. {
  37. nodes: node,
  38. },
  39. ),
  40. );
  41. return;
  42. }
  43. if (schemaDefinitionsCount > 0) {
  44. context.reportError(
  45. new GraphQLError('Must provide only one schema definition.', {
  46. nodes: node,
  47. }),
  48. );
  49. }
  50. ++schemaDefinitionsCount;
  51. },
  52. };
  53. }