gqlgen icon indicating copy to clipboard operation
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:

Open melkamu-zinabu opened this issue 1 year ago • 5 comments

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
    	},
    },
    

    })

melkamu-zinabu avatar Oct 12 '23 15:10 melkamu-zinabu

please help me

melkamu-zinabu avatar Oct 12 '23 15:10 melkamu-zinabu

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

suryatresna avatar Oct 15 '23 15:10 suryatresna

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

aleksey-korolev avatar Aug 09 '24 15:08 aleksey-korolev