LoneSchemaDefinition.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.schemaDefinitionNotAloneMessage = schemaDefinitionNotAloneMessage;
  6. exports.canNotDefineSchemaWithinExtensionMessage = canNotDefineSchemaWithinExtensionMessage;
  7. exports.LoneSchemaDefinition = LoneSchemaDefinition;
  8. var _GraphQLError = require("../../error/GraphQLError");
  9. function schemaDefinitionNotAloneMessage() {
  10. return 'Must provide only one schema definition.';
  11. }
  12. function canNotDefineSchemaWithinExtensionMessage() {
  13. return 'Cannot define a new schema within a schema extension.';
  14. }
  15. /**
  16. * Lone Schema definition
  17. *
  18. * A GraphQL document is only valid if it contains only one schema definition.
  19. */
  20. function LoneSchemaDefinition(context) {
  21. var oldSchema = context.getSchema();
  22. var alreadyDefined = oldSchema && (oldSchema.astNode || oldSchema.getQueryType() || oldSchema.getMutationType() || oldSchema.getSubscriptionType());
  23. var schemaDefinitionsCount = 0;
  24. return {
  25. SchemaDefinition: function SchemaDefinition(node) {
  26. if (alreadyDefined) {
  27. context.reportError(new _GraphQLError.GraphQLError(canNotDefineSchemaWithinExtensionMessage(), node));
  28. return;
  29. }
  30. if (schemaDefinitionsCount > 0) {
  31. context.reportError(new _GraphQLError.GraphQLError(schemaDefinitionNotAloneMessage(), node));
  32. }
  33. ++schemaDefinitionsCount;
  34. }
  35. };
  36. }