Skip to content
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

Closed
frederikhors opened this issue Nov 4, 2018 · 11 comments
Closed

A recipe for Gin integration? #413

frederikhors opened this issue Nov 4, 2018 · 11 comments
Labels
good first issue Good for newcomers low-priority Not currently a priority for the core team, but contributions are welcome

Comments

@frederikhors
Copy link
Collaborator

A recipe for Gin integration?

@sandeepone
Copy link

IMO you don't need another framework for running the Graphql server. It's always better to use net/http.

@gissleh
Copy link
Contributor

gissleh commented Nov 5, 2018

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?

@vektah vektah added low-priority Not currently a priority for the core team, but contributions are welcome good first issue Good for newcomers labels Nov 5, 2018
@vektah
Copy link
Collaborator

vektah commented Nov 5, 2018

I don't use gin, contributions welcome though :)

@rohmanhm
Copy link

rohmanhm commented Nov 8, 2018

There is go-chi implementation for cors, maybe you can get something there.

Ref:

@frederikhors
Copy link
Collaborator Author

frederikhors commented Nov 8, 2018

@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 #418 for this problem).

@vektah
Copy link
Collaborator

vektah commented Dec 3, 2018

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)))

@frederikhors
Copy link
Collaborator Author

frederikhors commented Dec 3, 2018

@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?

@appleboy
Copy link
Contributor

appleboy commented Dec 9, 2018

https://github.com/appleboy/golang-graphql-benchmark/blob/52716c04e3777b66a329280ef1856740187d949d/golang/gqlgen/server/server.go#L1-L28

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()
}

@jedhu0
Copy link

jedhu0 commented Feb 14, 2019

Is there any method to use middleware by gin.Context ?

@jonatasbaldin
Copy link
Contributor

Hey @huoshiqiu, added this at #676, what do you think?

@LouisAldorio
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers low-priority Not currently a priority for the core team, but contributions are welcome
Projects
None yet
Development

No branches or pull requests

10 participants