Haru Blank
Posted on March 23, 2024
Hello everyone,
I'm currently delving into learning Apollo Client. I've set up GraphQL with Go and gqlgen, and while queries and mutations are working fine, I'm encountering an issue when trying to connect to subscriptions on the client side. I keep receiving a network error.
Here's my go server:
import (
"net/http"
"server/graph"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/handler/transport"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/go-chi/chi"
"github.com/gorilla/websocket"
"github.com/rs/cors"
)
func main() {
router := chi.NewRouter()
router.Use(cors.New(cors.Options{
AllowedOrigins: []string{"http://localhost:5173"},
AllowCredentials: true,
Debug: true,
}).Handler)
srv := handler.NewDefaultServer(
graph.NewExecutableSchema(
graph.Config{
Resolvers: &graph.Resolver{}}))
srv.AddTransport(&transport.Websocket{
Upgrader: websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
// Check against your desired domains here
return true
},
ReadBufferSize: 1024,
WriteBufferSize: 1024,
},
})
router.Handle("/", playground.Handler("Graphql playground", "/query"))
router.Handle("/query", srv)
err := http.ListenAndServe(":8080", router)
if err != nil {
panic(err)
}
}
Could anyone provide assistance in resolving this issue? I would greatly appreciate any guidance or insights you can offer.
Thank you!
Here's Github repo: https://github.com/haruBlank00/graphql-go-todo
💖 💪 🙅 🚩
Haru Blank
Posted on March 23, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.