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

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more