簡體   English   中英

“訂閱字段必須返回異步可迭代。 收到:未定義”

[英]apollo “Subscription field must return Async Iterable. Received: undefined”

我有一個觸發通道事件'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

在此處輸入圖片說明 在此處輸入圖片說明 在此處輸入圖片說明 在此處輸入圖片說明 在此處輸入圖片說明 在此處輸入圖片說明 在此處輸入圖片說明 前端: 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

是模式: 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中的#入門指南后,這是服務日志

[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

我對一堆東西進行了分解/分解,以便更輕松地跟蹤正在發生的事情,但仍然遇到相同的錯誤

我在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. 客戶端必須使用terminatedLink和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,
  )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM