有人能告诉我如何在节点中使用Express-GraphQL实现GraphQL订阅吗?
发布于 2021-01-22 07:11:49
我也遇到了同样的问题。我无法在文档中找到明确的解决方案。所以我换了个瑜伽练习。但我找到了这条线所以一定要检查一下
发布于 2021-02-26 17:50:41
我一直在研究同样的问题。
我已经阅读了GitHub订阅的express-graphql问题,该回购组织的一位成员建议在结束评论时使用graphql-ws。
这里有一个指向我的GitHub项目三叶草/特快-graphql的链接,您可以通过npm start加载grapiql来测试查询和变异。
为了测试订阅,我创建了一个实现graphql-ws的可观察性示例. 三叶草/石墨rxjs rxjs-角的角项目。
uses项目还使用graphql-request进行查询和突变。
这是一个非常轻量级的解决方案,工作非常完美。
发布于 2022-08-09 07:08:17
他们在2020年11月通过一个示例实现添加了提到订阅支持的doc片段。但不幸的是,这一点从未被发布,这里提到了一个问题。
目前,我的解决方案已经转向了快车游乐场,用于subscriptions-transport-ws套接字(Playground还不支持graphql-ws )和阿波罗沙箱 ( graphql-ws )。
然后,我的订阅创建选项如下。
其中,createScopedPermissionWrapper只是一个带有@graphql-authz和createGraphqlContext的execute包装器,它是一个工厂函数,用于验证auth,并为我的解析器创建一个自定义上下文。
import { Server } from 'http'
import { useServer } from 'graphql-ws/lib/use/ws' // subscription with graphql-ws
import { SubscriptionServer } from 'subscriptions-transport-ws' // subscription with subscriptions-transport-ws
export const createSubscriptionsTransportWs = (server: Server) => {
const wsServer = new SubscriptionServer(
{
schema,
execute: createScopedPermissionWrapper(),
subscribe,
onConnect: (args: { authentication?: string }) =>
createGraphqlContext({
authentication: args.authentication,
}),
},
{ server, path }
)
const wsAddress = wsServer.server.address() as AddressInfo
depClients.logger.success(
`Graphql subscription socket up on ${wsAddress.address}:${wsAddress.port}${path}`
)
}
export const createGraphqlWS = (server: Server) => {
const wsServer = new ws.Server({ server, path })
useServer(
{
schema,
execute: createScopedPermissionWrapper(),
subscribe,
context: (args: { connectionParams: { authentication?: string } }) =>
createGraphqlContext({
authentication: args.connectionParams.authentication,
}),
},
wsServer
)
const wsAddress = wsServer.address() as AddressInfo
depClients.logger.success(
`Graphql subscription socket up on ${wsAddress.address}:${wsAddress.port}${path}`
)
}https://stackoverflow.com/questions/63134887
复制相似问题