我有一个触发通道事件'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入门指南之后的服务日志
[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
我对一堆东西进行了分解/分解,以便更轻松地跟踪正在发生的事情,但仍然会遇到相同的错误
答案 0 :(得分:4)
我在2个地方解决了这个问题
ApolloServer.installSubscriptionHandlers(ws)
const listener = ws.listen({port: config.PORT}, () => {
middleware.apolloSubscriptions(ws)
// middleware.apolloSubscriptions(ws)
private _terminatingLink = split(
({ query }) => {
const { kind, operation } = getMainDefinition(query)
return (
kind === 'OperationDefinition' && operation === 'subscription'
)
},
this._wsLink,
this._httpLink,
)