【问题标题】:Why am I not getting updates from my subscription?为什么我没有从我的订阅中获得更新?
【发布时间】:2022-01-22 00:12:30
【问题描述】:

我正在尝试设置 GraphQL Subscriptions,但它似乎已连接到后端,但没有推送任何更新。

在前端,我使用的是Nuxt 2,这就是我试图让它工作的方式:

这是我的测试查询


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

我在页面组件上的 smartQuery


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

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

后端:

我的发布订阅

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)
})

我的架构:

extend type Subscription {
  countPendingInquires: PendingInquires!
}

type PendingInquires {
    amount: Int!
}

我的解析器


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

这就是我试图推送事件的方式:

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

我还想知道是否有任何内置方法可以设置订阅的初始状态。

【问题讨论】:

    标签: redis graphql apollo apollo-server graphql-subscriptions


    【解决方案1】:

    问题在于我尝试推送事件的方式

    正确的推送方式是这样的:

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

    我刚刚设置了错误的订阅名称

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-22
      • 1970-01-01
      • 1970-01-01
      • 2021-08-16
      • 2021-07-26
      • 1970-01-01
      • 2018-03-31
      • 1970-01-01
      相关资源
      最近更新 更多