LoneSchemaDefinition.mjs 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. import { GraphQLError } from '../../error/GraphQLError';
  2. export function schemaDefinitionNotAloneMessage() {
  3. return 'Must provide only one schema definition.';
  4. }
  5. export function canNotDefineSchemaWithinExtensionMessage() {
  6. return 'Cannot define a new schema within a schema extension.';
  7. }
  8. /**
  9. * Lone Schema definition
  10. *
  11. * A GraphQL document is only valid if it contains only one schema definition.
  12. */
  13. export function LoneSchemaDefinition(context) {
  14. var oldSchema = context.getSchema();
  15. var alreadyDefined = oldSchema && (oldSchema.astNode || oldSchema.getQueryType() || oldSchema.getMutationType() || oldSchema.getSubscriptionType());
  16. var schemaDefinitionsCount = 0;
  17. return {
  18. SchemaDefinition: function SchemaDefinition(node) {
  19. if (alreadyDefined) {
  20. context.reportError(new GraphQLError(canNotDefineSchemaWithinExtensionMessage(), node));
  21. return;
  22. }
  23. if (schemaDefinitionsCount > 0) {
  24. context.reportError(new GraphQLError(schemaDefinitionNotAloneMessage(), node));
  25. }
  26. ++schemaDefinitionsCount;
  27. }
  28. };
  29. }