gpt4 book ai didi

javascript - Apollo "Subscription field must return Async Iterable. Received: undefined"

转载 作者:数据小太阳 更新时间:2023-10-29 04:16:29 26 4
gpt4 key购买 nike

我有一个触发 channel 事件“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 Controller :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 started 说明后的服务日志

[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 设置 将更新推送到堆栈。

更新 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. 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. terminatingLink 和 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,
)

关于javascript - Apollo "Subscription field must return Async Iterable. Received: undefined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56083422/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com