简体   繁体   中英

Getting WebSocket connection to 'ws://localhost:7080/query' failed on connecting graphql-ws client with gqlgen golang server

The subscription query listed below in code working fine on graphql playground but when i tried to connect my graphql-ws client with gqlgen driven go server, i got WebSocket connection to 'ws://localhost:7080/query' failed error. I tried connecting to my other localhost server running locally using useServer function of graphql-ws that worked nicely but got issue while connecting with go server.

Client Code:-

(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)
            }
   })();  

Slogging since last two days to get some useful blog and link online but not found anything useful of connecting go server with vanilla js. Also searched listed issues on graphql-ws library on its github page but there also no such issue is listed.

Please let me know if other info is required regarding this.

If you are talking about this gqlgen , it does not support theGraphQL over WebSocket Protocol graphql-ws implements. It, sadly, supports only the legacy protocol of subscriptions-transport-ws . (which is why graphql-playground works). Meaning, you must use the subscriptions-transport-ws with their backend.

The ws subprotocol of gqlgen is graphql-transport-ws since gqlgen@v0.15.0 . If someone is still facing connection problem at the "helloworld", check the checkOrigin property of websocket's upgrader.

I'm using gqlgen@v0.17.22 and have found out that the offical example of Websocket/Subscription can't config the checkOrigin property. The problem is that the handler.NewDefaultServer already have a configured ws transport, we could use handler.New and do the rest parts ourselves.

srv := handler.New(graphql.NewSchema(client))
srv.AddTransport(transport.Options{})
srv.AddTransport(transport.GET{})
srv.AddTransport(transport.POST{})
srv.AddTransport(transport.MultipartForm{})
srv.SetQueryCache(lru.New(1000))
srv.Use(extension.Introspection{})
srv.Use(extension.AutomaticPersistedQuery{
    Cache: lru.New(100),
})
// add ws transport configured by ourselves
srv.AddTransport(&transport.Websocket{
    Upgrader: websocket.Upgrader{
        //ReadBufferSize:  1024,
        //WriteBufferSize: 1024,
        CheckOrigin: func(r *http.Request) bool {
            // add checking origin logic to decide return true or false
            return true
        },
    },
    KeepAlivePingInterval: 10 * time.Second,
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM