gqlgen
gqlgen copied to clipboard
when i setup subscription endpoint(websocket connection) in my gqlgen + go code, it display an error as unable to upgrade *http.response to websocket websocket: request origin not allowed by Upgrader.CheckOrigin:
when i try to start websocket connection from frontend code,it display an error as unable to upgrade *http.response to websocket websocket: request origin not allowed by Upgrader.CheckOrigin in the server side to fix it i update the package but it still persist
-
go version
? -
// Create the GraphQL server srv := handler.NewDefaultServer(graph.NewExecutableSchema(graph.Config{ Resolvers: resolver, })) srv.AddTransport(&transport.Websocket{ Upgrader: websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool { // Replace "http://example.com" with the allowed origin allowedOrigin := "http://localhost:5173/" return r.Header.Get("Origin") == allowedOrigin }, },
})
please help me
I've got the same issue. My problem was solved with the change using New
instead of NewDefaultServer
something like this
srv := handler.New(graph.NewExecutableSchema(graph.Config{
Resolvers: resolver,
}))
If you use NewDefaultServer
https://github.com/99designs/gqlgen/blob/master/graphql/handler/server.go#L35-L37, that can't override because WebSocket already initialize
Origin header may be different or even missing depending on your frontend environment e.g. browser vs Postman client vs etc etc. You may have a look at default implementation of CheckOrigin to get better understanding how you'd like to customise this check: https://github.com/gorilla/websocket/blob/5e002381133d322c5f1305d171f3bdd07decf229/server.go#L87