LoneSchemaDefinitionRule.js 1.7 KB

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