DEV Community

Cover image for Todo app with go and gqlgen. Just need to add socket now
Haru Blank
Haru Blank

Posted on

Todo app with go and gqlgen. Just need to add socket now

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.

Error Message for goql subscription

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)
    }
}
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)