[英]How do I make GraphQL subscriptions in NodeJS via Apollo Client?
I want to make a subscription to a GraphQL server.我想订阅一个 GraphQL 服务器。 The application is running in a NodeJS script (ie not in the webbrowser).
该应用程序在 NodeJS 脚本中运行(即不在网络浏览器中)。
Here is what I currently do:这是我目前所做的:
const fetch = require("node-fetch").default;
const apollo = require("apollo-boost");
const ApolloClient = apollo.default;
const { gql } = require("apollo-server");
const apolloClient = new ApolloClient({
uri: "http://localhost:4000/graphql",
fetch
});
apolloClient.subscribe({
query: gql`
subscription {
startTaskRequested {
pkRobot
taskName
}
}
`,
}).subscribe({
next(x) { console.log(x) },
error(err) { console.log(`Finished with error: ${ err }`) },
complete() { console.log('Finished') }
});
The resulting ouput is:结果输出是:
{ data: { startTaskRequested: null } }
Finished
On the GraphQL Server, I can see that the corresponding resolver is never called.在 GraphQL Server 上,我可以看到相应的解析器从未被调用。
If I make the same subscription query using Apollo's Playground, the subscription works and I get the results that I expect: Apollo Playground如果我使用 Apollo 的 Playground 进行相同的订阅查询,则订阅会起作用并且我得到我期望的结果: Apollo Playground
I bumped my head against this for many hours already and I would greatly appreciate it if someone could point me in the right direction.我已经在这个问题上撞了好几个小时,如果有人能指出我正确的方向,我将不胜感激。
Alright, meanwhile I found a fantastic github repository, which contains the answer to my question: https://github.com/hasura/nodejs-graphql-subscriptions-boilerplate好的,同时我找到了一个很棒的 github 存储库,其中包含我的问题的答案: https : //github.com/hasura/nodejs-graphql-subscriptions-boilerplate
I'm still not sure why my code above is behaving in this way.我仍然不确定为什么我上面的代码会以这种方式运行。 If someone is able to explain that, I would appreciate it :-).
如果有人能够解释这一点,我将不胜感激:-)。
我已经使用 Apollo Client 通过 Appsync 运行订阅 - 使用 ApolloHttpLink 和 ApolloAuthLink 包,因此可用于任何基于 WS 的 graphql 订阅 - 试试这个 - https://github.com/kodehash/appsync-nodejs-apollo-client /树/主人
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.