【问题标题】:Subscription field must return Async Iterable. Received: [function]订阅字段必须返回 Async Iterable。收到:[功能]
【发布时间】:2022-04-05 12:11:57
【问题描述】:

我正在尝试过滤订阅以确保每个客户端只接收他们需要的数据。但是,当我使用此实现时出现错误

/**
 * Relevant packages and their versions
 *     "apollo-server-core": "^3.6.2",
 *     "apollo-server-koa": "^3.6.2",
 *     "graphql": "^16.3.0",
 *     "graphql-request": "^3.7.0",
 *     "graphql-subscriptions": "^2.0.0",
 *
 *
 */
const Subscription = {
  agentLocation: {
    type: AgentLocationType,
    args: {
      id: { type: GraphQLID },
    },
    resolve: (payload) => {
      console.log(payload, ' is the payload')
      return payload.agentLocation
    },
    subscribe: withFilter(
      () => pubsub.asyncIterator('LOCATION_UPDATED'),
      (payload, variables) => {
        return payload.agentLocation.id === variables.id
      }
    ),
  },
}

Apollo studio 显示此错误

我做错了什么?

【问题讨论】:

  • 我使用 withFilter 遇到了完全相同的问题,但使用的是 graphql-yoga。你找到解决办法了吗?
  • @Scott,检查我给出的答案是否有帮助

标签: graphql apollo-server graphql-subscriptions


【解决方案1】:

我不记得我到底做了什么,但我现在拥有的对我有用的是这个 sn-p。虽然我没有使用 graphql-yoga

const { PubSub, withFilter } = require('graphql-subscriptions')

const { GraphQLID } = require('graphql')
const pubsub = new PubSub()
const Subscription = {
  agentLocation: {
    type: AgentLocationType,
    args: {
      id: { type: GraphQLID },
    },
    resolve: (payload) => {
      return payload.agentLocation
    },
    subscribe: withFilter(
      () => pubsub.asyncIterator('LOCATION_UPDATED'),
      (payload, variables) => {
        return payload.agentLocation.id === variables.id
      }
    ),
  },
}

【讨论】:

    猜你喜欢
    • 2021-07-06
    • 2020-04-04
    • 2019-09-28
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    • 2021-02-15
    • 1970-01-01
    • 2021-05-26
    相关资源
    最近更新 更多