【问题标题】:Getting WebSocket connection to 'ws://localhost:7080/query' failed on connecting graphql-ws client with gqlgen golang server在将 graphql-ws 客户端与 gqlgen golang 服务器连接时,获取到“ws://localhost:7080/query”的 WebSocket 连接失败
【发布时间】:2021-08-12 22:30:28
【问题描述】:

下面列出的订阅查询在 graphql playground 上运行良好,但是当我尝试将我的 graphql-ws 客户端与 gqlgen 驱动的 go 服务器连接时,我得到 WebSocket 连接到 'ws: //localhost:7080/query' failed 错误。我尝试使用 graphql-ws 的 useServer 功能 连接到本地运行的其他 localhost 服务器,该功能运行良好,但在连接 go 服务器时出现问题。

客户代码:-

(async () => {
 const client = graphqlWs.createClient({
            url: 'ws://localhost:7080/query',
        });


 const onNext = (data) => {
          /* handle incoming values */
          console.log("data===>", data)
  };

    await new Promise((resolve, reject) => {
                unsubscribe = client.subscribe(
                    {
                        query: `subscription {
                            ping
                          }`
                       
                    },
                    {
                        next: onNext,
                        error: reject,
                        complete: resolve
                    },
                );
            });
            } catch(e) {
                console.error("E==>", e)
            }
   })();  

自过去两天以来一直在努力获取一些有用的博客和在线链接,但没有发现任何将 go 服务器与 vanilla js 连接起来的有用信息。还在其 github 页面上搜索了 graphql-ws 库中列出的问题,但也没有列出此类问题。

如果需要其他信息,请告诉我。

【问题讨论】:

    标签: go websocket graphql graphql-subscriptions gqlgen


    【解决方案1】:

    如果你说的是this gqlgen,它不支持GraphQL over WebSocket Protocol graphql-ws 实现。 It, sadly, supports only the legacy protocol of subscriptions-transport-ws.(这就是 graphql-playground 起作用的原因)。这意味着,您必须将subscriptions-transport-ws 与他们的后端一起使用。

    【讨论】:

    • 是的,我只使用了这个 gqlgen,但是在上面 github 链接的 L65 中,它写成 "Sec-Websocket-Protocol": []string{"graphql-ws"},,所以这里提到了 graphql-ws。我是不是理解错了。那么,这行"Sec-Websocket-Protocol": []string{"graphql-ws"},是什么意思
    • the new GraphQL over WebSocket is graphql-transport-ws 的子协议,不是graphql-ws(属于subscriptions-transport-ws)。
    猜你喜欢
    • 2023-01-30
    • 2023-02-14
    • 2016-06-28
    • 2023-01-17
    • 1970-01-01
    • 2017-11-15
    • 2023-01-19
    • 2021-07-08
    • 2022-07-14
    相关资源
    最近更新 更多