NestJS GraphQL 订阅不适用于 `graphql-ws`
NestJS GraphQL subscriptions not working with `graphql-ws`
我正在尝试升级我们的 NestJS GraphQL 订阅服务器以利用 graphql-ws
而不是当前的 subscriptions-transport-ws
(如 NestJS documentation 所建议)。
我将 NestJS 版本升级到
"@nestjs/core": "^8.0.6",
"@nestjs/graphql": "^9.0.4",
"@nestjs/platform-express": "^8.0.6",
"graphql": "^15.5.3",
"graphql-tools": "^8.2.0",
"apollo-server-express": "^3.3.0",
之后,我将 subscriptions
选项添加到 App.Module
:
GraphQLModule.forRoot({
autoSchemaFile: true,
sortSchema: true,
playground: true,
installSubscriptionHandlers: true,
subscriptions: {
'graphql-ws': true
},
}),
然而,当我(在操场上)订阅以前有效的订阅时,我得到:
{
"error": "Could not connect to websocket endpoint ws://localhost:8880/graphql. Please check if the endpoint url is correct."
}
在控制台中我得到:
WebSocket protocol error occured. It was most likely caused due to an unsupported subprotocol "graphql-ws" requested by the client. graphql-ws implements exclusively the "graphql-transport-ws" subprotocol, please make sure that the client implements it too.
我尝试过的事情:
- 添加
graphql-ws
包
- 再次升级NestJS版本
- 正在从配置中删除
installSubscriptionHandlers
选项
- 设置
graphql-ws
配置而不是传递 true
- 使用
WebSocket Test Client
Google Chrome 扩展而不是 Playground
但是 none 起作用了。很抱歉 post。我该如何解决这个问题?
在 Apollo Server 3 发布时,GraphQL Playground 或 Apollo Explorer 尚不支持 graphql-ws 库中使用的协议。
见here
只有在通过 playground 与订阅交互对您没有多大用处时,才建议使用 graphql-ws,并且您可以仅从您自己的已设置为使用 graphql-ws 的客户端与订阅交互。
设置您的客户端以将 graphql-ws 与 Apollo 一起使用。参见 here。
这将完成工作:
subscriptions: {
'graphql-ws': true,
'subscriptions-transport-ws': true,
},
如doc所述:
HINT
You can also use both packages (subscriptions-transport-ws and graphql-ws) at > the same time, for example, for backward compatibility.
我正在尝试升级我们的 NestJS GraphQL 订阅服务器以利用 graphql-ws
而不是当前的 subscriptions-transport-ws
(如 NestJS documentation 所建议)。
我将 NestJS 版本升级到
"@nestjs/core": "^8.0.6",
"@nestjs/graphql": "^9.0.4",
"@nestjs/platform-express": "^8.0.6",
"graphql": "^15.5.3",
"graphql-tools": "^8.2.0",
"apollo-server-express": "^3.3.0",
之后,我将 subscriptions
选项添加到 App.Module
:
GraphQLModule.forRoot({
autoSchemaFile: true,
sortSchema: true,
playground: true,
installSubscriptionHandlers: true,
subscriptions: {
'graphql-ws': true
},
}),
然而,当我(在操场上)订阅以前有效的订阅时,我得到:
{
"error": "Could not connect to websocket endpoint ws://localhost:8880/graphql. Please check if the endpoint url is correct."
}
在控制台中我得到:
WebSocket protocol error occured. It was most likely caused due to an unsupported subprotocol "graphql-ws" requested by the client. graphql-ws implements exclusively the "graphql-transport-ws" subprotocol, please make sure that the client implements it too.
我尝试过的事情:
- 添加
graphql-ws
包 - 再次升级NestJS版本
- 正在从配置中删除
installSubscriptionHandlers
选项 - 设置
graphql-ws
配置而不是传递true
- 使用
WebSocket Test Client
Google Chrome 扩展而不是 Playground
但是 none 起作用了。很抱歉 post。我该如何解决这个问题?
在 Apollo Server 3 发布时,GraphQL Playground 或 Apollo Explorer 尚不支持 graphql-ws 库中使用的协议。
见here
只有在通过 playground 与订阅交互对您没有多大用处时,才建议使用 graphql-ws,并且您可以仅从您自己的已设置为使用 graphql-ws 的客户端与订阅交互。
设置您的客户端以将 graphql-ws 与 Apollo 一起使用。参见 here。
这将完成工作:
subscriptions: {
'graphql-ws': true,
'subscriptions-transport-ws': true,
},
如doc所述:
HINT
You can also use both packages (subscriptions-transport-ws and graphql-ws) at > the same time, for example, for backward compatibility.