SingleFieldSubscriptions.js 1000 B

12345678910111213141516171819202122232425262728293031
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.singleFieldOnlyMessage = singleFieldOnlyMessage;
  6. exports.SingleFieldSubscriptions = SingleFieldSubscriptions;
  7. var _GraphQLError = require("../../error/GraphQLError");
  8. function singleFieldOnlyMessage(name) {
  9. return name ? "Subscription \"".concat(name, "\" must select only one top level field.") : 'Anonymous Subscription must select only one top level field.';
  10. }
  11. /**
  12. * Subscriptions must only include one field.
  13. *
  14. * A GraphQL subscription is valid only if it contains a single root field.
  15. */
  16. function SingleFieldSubscriptions(context) {
  17. return {
  18. OperationDefinition: function OperationDefinition(node) {
  19. if (node.operation === 'subscription') {
  20. if (node.selectionSet.selections.length !== 1) {
  21. context.reportError(new _GraphQLError.GraphQLError(singleFieldOnlyMessage(node.name && node.name.value), node.selectionSet.selections.slice(1)));
  22. }
  23. }
  24. }
  25. };
  26. }