SingleFieldSubscriptionsRule.mjs 711 B

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