javascript - 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
前端: 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>
)
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
更新
如果您尝试克隆 repo,但在运行 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
我解调/分解了一堆东西,以便更容易追踪正在发生的事情,但仍然得到相同的错误
解决方案
我在两个地方解决了这个问题
- 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)
- 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,
)
推荐阅读
- c# - Jaeger 服务:如何在 Google Kubernetes 上进行配置?
- java - 在javaFX中将DoubleBinding转换为double
- r - 从 df 中删除包含相同字符串的字符向量
- hadoop - 清除或删除 YARN (MR2) 应用程序队列
- mongodb - MongoDB 聚合 - 所有子对象的 elemMatch
- spring-boot - 动态注入没有注解的bean
- java - JDK11/EAP7 升级后 XML 解析中断
- c# - Acumatica:从另一个图表插入 POLine 的问题
- mongodb - 如何在 Windows 10 上更改 mongodb 的 dbpath 位置?
- node.js - 在 Windows 启动时通过电子应用程序打开文件时出错