Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apollo Server Subscription subscribe method is never called

I have an existing Express ApolloServer . I added subscription to that . I can see when I fire the subscription from Playground, the resolve method is called . But, the subscribe method is never called


const { PubSub, withFilter } = require ('apollo-server');
const pubsub = new PubSub();
const SOMETHING_CHANGED_TOPIC = 'something_changed';

const mySubscription = {

  Subscription: {
    somethingChanged: {
      resolve: root => {
        console.log('subscription server resolve', { root })
        return root
      },
      subscribe: () => {
        console.log('I AM HERE IN SUBSCRIPTION', pubsub.asyncIterator(SOMETHING_CHANGED_TOPIC))
        return pubsub.asyncIterator(SOMETHING_CHANGED_TOPIC)
      }
    }
  }
};

module.exports = { mySubscription}

I can see the console.log('subscription server resolve', { root }) getting printed although root is undefined. But the similar console.log('````') in subscribe is not executed .

like image 572
CoolOS Avatar asked Jul 31 '26 11:07

CoolOS


1 Answers

You need to call pubsub.publish somewhere (usually in one of your resolvers) to trigger the subscription.

https://www.apollographql.com/docs/apollo-server/data/subscriptions/#subscriptions-example

like image 171
benawad Avatar answered Aug 03 '26 05:08

benawad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!