2

When I tried to setup a Go web server with GraphQL I used this as template. It is basically a combo of gin and 99designs/gqlgen.

When I create a basic gqlgen server based on net/http package, the declaration of GraphQL subscriptions work as expected.

package main

import (
    "log"
    "net/http"
    "os"

    "github.com/99designs/gqlgen/graphql/handler"
    "github.com/99designs/gqlgen/graphql/playground"
    "github.com/jawil003/gqlgen/graph"
    "github.com/jawil003/gqlgen/graph/generated"
)

const defaultPort = "8080"

func main() {
    port := os.Getenv("PORT")
    if port == "" {
        port = defaultPort
    }

    srv := handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: &graph.Resolver{}}))

    http.Handle("/", playground.Handler("GraphQL playground", "/query"))
    http.Handle("/query", srv)

    log.Printf("connect to http://localhost:%s/ for GraphQL playground", port)
    log.Fatal(http.ListenAndServe(":"+port, nil))
}

But when I add gin, like this:

package main

import (
    "github.com/gin-gonic/gin"
    "github.com/jawil003/gqlgen-todos/graph"
    "github.com/jawil003/gqlgen-todos/graph/generated"

    "github.com/99designs/gqlgen/graphql/handler"
    "github.com/99designs/gqlgen/graphql/playground"
)

// Defining the Graphql handler
func graphqlHandler() gin.HandlerFunc {
    // NewExecutableSchema and Config are in the generated.go file
    // Resolver is in the resolver.go file
    h := handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: &graph.Resolver{}}))

    return func(c *gin.Context) {
        h.ServeHTTP(c.Writer, c.Request)
    }
}

// Defining the Playground handler
func playgroundHandler() gin.HandlerFunc {
    h := playground.Handler("GraphQL", "/query")

    return func(c *gin.Context) {
        h.ServeHTTP(c.Writer, c.Request)
    }
}

func main() {
    // Setting up Gin
    r := gin.Default()
    r.POST("/query", graphqlHandler())
    r.GET("/", playgroundHandler())
    r.Run()
}

I get this issue:

{ "error": "Could not connect to websocket endpoint ws://localhost:8080/query. Please check if the endpoint url is correct." }

Is there any known solution to make gin work with graphql subscriptions?

blackgreen
  • 34,072
  • 23
  • 111
  • 129
Willey3x37
  • 310
  • 2
  • 12
  • 2
    How are you making the requests? One key difference between the two examples is that `http.Handle` is method-agnostic, whereas in Gin you are declaring only `POST` handler for `/query` route. Try changing to `r.Any` – blackgreen Oct 04 '21 at 11:49
  • Thanks a lot. The Issue was that the /query Endpoint needs to be exposed via GET as well... I think that has something to with how subscriptions work here. – Willey3x37 Oct 04 '21 at 12:01

1 Answers1

1

Hello to fix error Could not connect to websocket endpoint.. with Gin change r.POST("/query", graphqlHandler()) to r.Any("/query", graphqlHandler())

eclaude
  • 846
  • 14
  • 30