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

Fal*_*son 8 javascript typescript apollo reactjs graphql

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

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

我收到了graphqlPlayground错误

"Subscription field must return Async Iterable. Received: undefined"
Run Code Online (Sandbox Code Playgroud)

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

在没有TGRstack的情况下进行工作复制: https : //github.com/Falieson/fullstack-apollo-subscription-example

在此处输入图片说明 在此处输入图片说明 在此处输入图片说明 在此处输入图片说明 在此处输入图片说明 在此处输入图片说明 在此处输入图片说明 前端: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>
)
Run Code Online (Sandbox Code Playgroud)

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

是模式: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
}
Run Code Online (Sandbox Code Playgroud)
async function countIncr(root: any, args: any, context: any) {
  const count = Counter.increment()
  await pubsub.publish('countIncr', count )
  console.log('countIncr', '>>>', { count })
  return count
}
Run Code Online (Sandbox Code Playgroud)

阅读完Readme.md中的#入门指南后,这是服务日志

[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
Run Code Online (Sandbox Code Playgroud)

更新

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

更新2

每次最新提交时更新的代码和有问题的链接

更新3

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

Error: Apollo Server requires either an existing schema, modules or typeDefs
Run Code Online (Sandbox Code Playgroud)

更新4

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

"message": "Subscription field must return Async Iterable. Received: undefined"
Run Code Online (Sandbox Code Playgroud)

在没有TGRstack的情况下进行工作复制:https : //github.com/Falieson/fullstack-apollo-subscription-example

更新5

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

Fal*_*son 4

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

  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)
Run Code Online (Sandbox Code Playgroud)
  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,
  )
Run Code Online (Sandbox Code Playgroud)