LoneAnonymousOperationRule.mjs 842 B

123456789101112131415161718192021222324
  1. import { GraphQLError } from "../../error/GraphQLError.mjs";
  2. import { Kind } from "../../language/kinds.mjs";
  3. /**
  4. * Lone anonymous operation
  5. *
  6. * A GraphQL document is only valid if when it contains an anonymous operation
  7. * (the query short-hand) that it contains only that one operation definition.
  8. */
  9. export function LoneAnonymousOperationRule(context) {
  10. var operationCount = 0;
  11. return {
  12. Document: function Document(node) {
  13. operationCount = node.definitions.filter(function (definition) {
  14. return definition.kind === Kind.OPERATION_DEFINITION;
  15. }).length;
  16. },
  17. OperationDefinition: function OperationDefinition(node) {
  18. if (!node.name && operationCount > 1) {
  19. context.reportError(new GraphQLError('This anonymous operation must be the only defined operation.', node));
  20. }
  21. }
  22. };
  23. }