subscriptions in graphql-yoga

327 Views Asked by At

In the pubSub-section of graphql-yoga doc, under the randomNumber of Subscription resolver(server.ts), what does the resolve function i.e resolve: (payload) => payload do? and what is payload?

1

There are 1 best solutions below

0
On
  • resolve function helps you to perform certain operations on your payload.

  • payload is the object that you want to publish, which is provided as the last argument in the pubsub.publish method.

for.eg.

type Mutation {
    count(data: Int): Int!
}
type Subscription {
    count: Int!
}
const Mutation = {
    count: (parent, args, { pubsub }, info) => {
        pubsub.publish("count", {
            count: 1,
        });
        return 1;
    },
};

const Subscription = {
    count: {
        subscribe: (parent, args, { pubsub }, info) => {
            return pubsub.subscribe("count");
        },
        resolve: (payload) => payload.count * 2,
    },
};

This prints out the following to the subscriber:

{
  "data": {
    "count": 2
  }
}

Without the "resolve" function it would have printed out:

{
  "data": {
    "count": 1
  }
}