-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
A recipe for Gin integration? #413
Comments
IMO you don't need another framework for running the Graphql server. It's always better to use |
I don't use Gin, but I took a quick look at it. It appears that you can get a |
I don't use gin, contributions welcome though :) |
There is go-chi implementation for Ref: |
Thanks for your opinion, this is an old battle. I need
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 #418 for this problem). |
I would just create the handler once and mount it twice, so the caches are shared.
|
@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 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? |
gin router integration with Gql gen cause a problem when you try to use federation with it, it's like the gateway can miss behave and dunno go where to forward the federation |
A recipe for Gin integration?
The text was updated successfully, but these errors were encountered: