apollo“订阅字段必须返回异步可迭代。已接收:未定义”

时间:2019-05-10 18:58:49

标签: javascript reactjs typescript graphql apollo

我有一个触发通道事件'countIncr'的突变,但是我看不到活动的相应订阅事件与事件有效负载一起触发。

更新:我对此发布内容进行了多次更新,现在我更改标题以更加代表我的位置。

我遇到了graphqlPlayground错误

"Subscription field must return Async Iterable. Received: undefined"

TGRstack复制我遇到了问题: https://github.com/TGRstack/tgr-apollo-subscription-example-microservice/

不使用TGRstack进行工作复制: https://github.com/Falieson/fullstack-apollo-subscription-example

enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here 前端: https://github.com/TGRstack/tgr-apollo-subscription-example-microservice/blob/master/counter-ui/src/app/routes/Home/HomePage.tsx

const COUNTER_SUBSCRIPTION = gql`
subscription onCountIncr {
  count
}
`

const Counter = () => (
  <Subscription
    subscription={COUNTER_SUBSCRIPTION}
  >
    {({ data, loading }) => {
      console.log({loading, data})
      return loading
        ? <h1>Loading ...</h1>
        : data.count
          ? <h2>Counter: {data.count}</h2>
          : <h1>Counter Subscription Not Available</h1>
    }}
  </Subscription>
)

BE解析器:https://github.com/TGRstack/tgr-apollo-subscription-example-microservice/blob/master/counter-service/src/gql/Resolvers.ts

BE模式:https://github.com/TGRstack/tgr-apollo-subscription-example-microservice/blob/master/counter-service/src/gql/Schema.ts

BE控制器:https://github.com/TGRstack/tgr-apollo-subscription-example-microservice/blob/master/counter-service/src/gql/Counter.ts

const count = {
  resolve: data => {
    console.log('CounterSub>', {data})
    return data
  },
  subscribe: () => pubsub.asyncIterator(['countIncr'])
}

const CounterSubscriptions = {
  count
}
async function countIncr(root: any, args: any, context: any) {
  const count = Counter.increment()
  await pubsub.publish('countIncr', count )
  console.log('countIncr', '>>>', { count })
  return count
}

这里是您阅读Readme.md中的#getting入门指南之后的服务日志

[FE] GET /favicon.ico 200 2.465 ms - 1551                   # WEBCLIENT LOADED
[BE] CounterSub> { data: undefined }                        # SUBSCRIPTION REQUEST
[BE] { data: [Object: null prototype] { count: null } }     # SUBSCRIPTION RESULT
[BE] POST / 200 21.254 ms - 24
[BE] 2019-05-10 11:37:20 [info]:     HELLO                  # APOLLO CLIENT CONNECTED AGAIN (why always 2?)
[BE] countIncr >>> { count: 1 }                             # MUTATION REQUEST
[BE] { data: [Object: null prototype] { countIncr: 1 } }    # MUTATION RESPONSE
[BE] POST / 200 13.159 ms - 25
[BE] countIncr >>> { count: 2 }                             # MUTATION REQUEST
[BE] { data: [Object: null prototype] { countIncr: 2 } }    # MUTATION RESPONSE
[BE] POST / 200 4.380 ms - 25

更新

如果您尝试克隆存储库,并且在运行nps后无法正常运行,因为nps setup中缺少一个步骤。我已经改进了nps setup,将更新推送到堆栈中。

更新2

最新提交的代码和有问题的链接

更新3

有人建议pubsub应该是一次导入。我已经更新了代码,但这会创建一个新错误:

Error: Apollo Server requires either an existing schema, modules or typeDefs

更新4

尝试查找导入/导出错误(?)的大量次要更改现在得到了错误。我通过强化导入来解决此错误(存在索引文件无法正确导出的问题。

"message": "Subscription field must return Async Iterable. Received: undefined"

不使用TGRstack进行工作复制:https://github.com/Falieson/fullstack-apollo-subscription-example

更新5

我对一堆东西进行了分解/分解,以便更轻松地跟踪正在发生的事情,但仍然会遇到相同的错误

1 个答案:

答案 0 :(得分:4)

我在2个地方解决了这个问题

  1. ApolloServer.installSubscriptionHandler()临时替换Middleware.apolloSubscriptions()。我按照此指南配置了订阅中间件:https://www.apollographql.com/docs/graphql-subscriptions/express,所以我想可能是其中一个软件包的版本或指南本身搞砸了。

    ApolloServer.installSubscriptionHandlers(ws)

    const listener = ws.listen({port: config.PORT}, () => {
      middleware.apolloSubscriptions(ws)
      // middleware.apolloSubscriptions(ws)
  1. terminationLink和getMainDefinition对于客户端是必需的 https://github.com/TGRstack/tgr-apollo-subscription-example-microservice/commit/75b6165f2dc1d035a41f1129f7386a1e18c7ba53#diff-2c47ef33b8ed0e4c893cbc161bcf7814R37
  private _terminatingLink = split(
    ({ query }) => {
      const { kind, operation } = getMainDefinition(query)
      return (
        kind === 'OperationDefinition' && operation === 'subscription'
      )
    },
    this._wsLink,
    this._httpLink,
  )