LoneAnonymousOperationRule.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. * See https://spec.graphql.org/draft/#sec-Lone-Anonymous-Operation
  15. */
  16. function LoneAnonymousOperationRule(context) {
  17. let operationCount = 0;
  18. return {
  19. Document(node) {
  20. operationCount = node.definitions.filter(
  21. (definition) => definition.kind === _kinds.Kind.OPERATION_DEFINITION,
  22. ).length;
  23. },
  24. OperationDefinition(node) {
  25. if (!node.name && operationCount > 1) {
  26. context.reportError(
  27. new _GraphQLError.GraphQLError(
  28. 'This anonymous operation must be the only defined operation.',
  29. {
  30. nodes: node,
  31. },
  32. ),
  33. );
  34. }
  35. },
  36. };
  37. }