LoneAnonymousOperationRule.js 1002 B

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