LoneAnonymousOperation.mjs 922 B

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