SingleFieldSubscriptions.mjs 799 B

123456789101112131415161718192021
  1. import { GraphQLError } from '../../error/GraphQLError';
  2. export function singleFieldOnlyMessage(name) {
  3. return name ? "Subscription \"".concat(name, "\" must select only one top level field.") : 'Anonymous Subscription must select only one top level field.';
  4. }
  5. /**
  6. * Subscriptions must only include one field.
  7. *
  8. * A GraphQL subscription is valid only if it contains a single root field.
  9. */
  10. export function SingleFieldSubscriptions(context) {
  11. return {
  12. OperationDefinition: function OperationDefinition(node) {
  13. if (node.operation === 'subscription') {
  14. if (node.selectionSet.selections.length !== 1) {
  15. context.reportError(new GraphQLError(singleFieldOnlyMessage(node.name && node.name.value), node.selectionSet.selections.slice(1)));
  16. }
  17. }
  18. }
  19. };
  20. }