I am working with Apollo to make it possible to trigger Subscription at a later time by scheduling. I have chosen to use Agenda to achieve scheduling in my NodeJS server.

I where thinking maybe I could use the pubsub const with the same id 'newNotification' to trigger the subscription. But the subscription is not fired, only the Agenda event is.

My Agenda function:

const schedule = async (options) => {

    const {name, time, data} = options

    // Don't do anything until agenda server is ready

    await new Promise(resolve => agenda.once('ready', resolve));

    // define the agenda type
    agenda.define(name, (job, done) => {
        const {user} = job.attrs.data;
        console.log(`Hello, World! ${user}`);
        pubsub.publish('newNotification', {}, { user: user });
        done()
    });

    // Define and serve the agenda
    agenda.schedule(time, name, data);

}

schedule({
  name: "My event",
  time: moment().add(20, 's').toDate(),
  data: {
    user: "5c920329756c52831cf45f0b"
  }
})

My Subscription resolver is like this:

    Subscription: {
        newNotification: {
            subscribe: withFilter(
                () => pubsub.asyncIterator('newNotification'),
                (payload, variables) => {
                    return payload.user === variables.user;
                },
            )
        }
    },

0 Answers