1

I am trying to set up GraphQL Subscriptions but it seems to get connected to the backend but it's not pushing any updates.

On frontend, I am using Nuxt 2 and that's how I am trying to get it working:

That's my test query


export const pendingInquiresSubscription = () => {
    return gql`
        subscription PendingInquires {
            countPendingInquires {
                amount
            }
        }`
}

My smartQuery on the page component


 apollo: {
        $subscribe: {
            pendingInquires: {
                query: pendingInquiresSubscription(),
                result({ data, loading }) {
                    this.loading = loading;

                    console.log(data)
                },
                error(err) {
                    this.$notify({ message: `Что-то пошло не так пытаясь обновить количество новый запросов: ${err.message}`, type: 'error' })
                },
            }
        }
        
    },

That's what I am getting at network with 101

Backend:

my pubsub

import { RedisPubSub } from 'graphql-redis-subscriptions';
import Redis from 'ioredis';

const REDIS_DOMAIN_NAME = '127.0.0.1'
const PORT_NUMBER = 6379

const options = {
    host: REDIS_DOMAIN_NAME,
    port: PORT_NUMBER,
    retryStrategy: (times: any) => {
        return Math.min(times * 50, 2000);
    }
}

export const pubsub = new RedisPubSub({
    publisher: new Redis(options),
    subscriber: new Redis(options)
})

My Schema:

extend type Subscription {
  countPendingInquires: PendingInquires!
}

type PendingInquires {
    amount: Int!
}

My resolver


...
    Subscription: {
        countPendingInquires: {
            subscribe: () => pubsub.asyncIterator(['countPendingInquires'])
        },
    },
...

That's the way I am trying to push the event:

            pubsub.publish('countPendingInquires', {
                PendingInquires: {
                    amount: await TelegramInguireModel.find({ }).countDocuments()
                }
            })

And I also wonder if there is any built-in way to set the initial state for subscriptions.

1 Answers1

1

The issue was in the way I was trying to push the event

The correct way of pushing is like this:


pubsub.publish('countPendingInquires', {
    countPendingInquires: { // <- here was the issue
         amount: await TelegramInquireModel.find({ }).countDocuments()
    }
)

I've just set wrong subscription name