LoneAnonymousOperation.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.anonOperationNotAloneMessage = anonOperationNotAloneMessage;
  6. exports.LoneAnonymousOperation = LoneAnonymousOperation;
  7. var _GraphQLError = require("../../error/GraphQLError");
  8. var _kinds = require("../../language/kinds");
  9. function anonOperationNotAloneMessage() {
  10. return 'This anonymous operation must be the only defined operation.';
  11. }
  12. /**
  13. * Lone anonymous operation
  14. *
  15. * A GraphQL document is only valid if when it contains an anonymous operation
  16. * (the query short-hand) that it contains only that one operation definition.
  17. */
  18. function LoneAnonymousOperation(context) {
  19. var operationCount = 0;
  20. return {
  21. Document: function Document(node) {
  22. operationCount = node.definitions.filter(function (definition) {
  23. return definition.kind === _kinds.Kind.OPERATION_DEFINITION;
  24. }).length;
  25. },
  26. OperationDefinition: function OperationDefinition(node) {
  27. if (!node.name && operationCount > 1) {
  28. context.reportError(new _GraphQLError.GraphQLError(anonOperationNotAloneMessage(), node));
  29. }
  30. }
  31. };
  32. }