IMO you don't need another framework for running the Graphql server. It's always better to use net/http.
I don't use Gin, but I took a quick look at it. It appears that you can get a *http.Request and http.ResponseWriter from the gin.Context. Have you tried calling the graphql handler's ServerHTTP with those?
I don't use gin, contributions welcome though :)
There is go-chi implementation for cors, maybe you can get something there.
Ref:
@sandeepone
IMO you don't need another framework for running the Graphql server. It's always better to use net/http.
Thanks for your opinion, this is an old battle. I need gin for many reasons.
@gissleh
I don't use Gin, but I took a quick look at it. It appears that you can get a *http.Request and http.ResponseWriter from the gin.Context. Have you tried calling the graphql handler's ServerHTTP with those?
Maybe like the code below?
package server
import (
"myProject/graphql"
"github.com/99designs/gqlgen/handler"
"github.com/gin-gonic/gin"
)
func Start() {
r := gin.Default()
r.GET("/query", gin.WrapH(handler.Playground("GraphQL playground", "/api")))
r.GET("/api", gin.WrapH(handler.GraphQL(graphql.NewExecutableSchema(graphql.Config{Resolvers: &graphql.Resolver{}}))))
r.POST("/api", gin.WrapH(handler.GraphQL(graphql.NewExecutableSchema(graphql.Config{Resolvers: &graphql.Resolver{}}))))
r.Run()
}
@vektah can I ask if you see something wrong in the above code? Is it correct to repeat two times these lines?
r.GET("/api", gin.WrapH(handler.GraphQL(graphql.NewExecutableSchema(graphql.Config{Resolvers: &graphql.Resolver{}}))))
r.POST("/api", gin.WrapH(handler.GraphQL(graphql.NewExecutableSchema(graphql.Config{Resolvers: &graphql.Resolver{}}))))
(I opened https://github.com/99designs/gqlgen/issues/418 for this problem).
I would just create the handler once and mount it twice, so the caches are shared.
gql := handler.GraphQL(graphql.NewExecutableSchema(graphql.Config{Resolvers: &graphql.Resolver{}})
r.GET("/api", gin.WrapH(gql))
r.POST("/api", gin.WrapH(gql)))
@vektah what do you think?
It's the same if I use this code:
gql := gin.WrapH(handler.GraphQL(graphql.NewExecutableSchema(graphql.Config{Resolvers: &graphql.Resolver{}})))
r.GET("/api", gql)
r.POST("/api", gql)
instead of this one:
gql := handler.GraphQL(graphql.NewExecutableSchema(graphql.Config{Resolvers: &graphql.Resolver{}})
r.GET("/api", gin.WrapH(gql))
r.POST("/api", gin.WrapH(gql)))
_Differences_: in the first I'm reusing also gin.WrapH.
What about performance? Negligible?
package main
import (
"fmt"
"github.com/99designs/gqlgen/handler"
"github.com/appleboy/golang-graphql-benchmark/golang/gqlgen"
"github.com/gin-gonic/gin"
)
// Handler initializes the graphql middleware.
func Handler() gin.HandlerFunc {
// Creates a GraphQL-go HTTP handler with the defined schema
h := handler.GraphQL(gqlgen.NewExecutableSchema(gqlgen.Config{Resolvers: &gqlgen.Resolver{}}))
return func(c *gin.Context) {
h.ServeHTTP(c.Writer, c.Request)
}
}
func main() {
r := gin.New()
r.POST("/graphql", Handler())
fmt.Println("Now server is running on port 8080")
fmt.Println("Test with Get : curl -g 'http://localhost:8080/graphql?query={hello}'")
r.Run()
}
Is there any method to use middleware by gin.Context ?
Hey @huoshiqiu, added this at #676, what do you think?
Most helpful comment
https://github.com/appleboy/golang-graphql-benchmark/blob/52716c04e3777b66a329280ef1856740187d949d/golang/gqlgen/server/server.go#L1-L28