v0.13.0 master v0.12.2 v0.11.3 v0.10.2 v0.9.3 v0.8.3


Using Gin to setup HTTP handlers

Gin is an excellent alternative for the net/http router. From their official GitHub page:

Gin is a web framework written in Go (Golang). It features a martini-like API with much better performance, up to 40 times faster thanks to httprouter. If you need performance and good productivity, you will love Gin.

Here are the steps to setup Gin and gqlgen together:

Install Gin:

$ go get github.com/gin-gonic/gin

In your router file, define the handlers for the GraphQL and Playground endpoints in two different methods and tie then together in the Gin router:

import (


// 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(NewExecutableSchema(Config{Resolvers: &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())

Accessing gin.Context

At the Resolver level, gqlgen gives you access to the context.Context object. One way to access the gin.Context is to add it to the context and retrieve it again.

First, create a gin middleware to add its context to the context.Context:

func GinContextToContextMiddleware() gin.HandlerFunc {
	return func(c *gin.Context) {
		ctx := context.WithValue(c.Request.Context(), "GinContextKey", c)
		c.Request = c.Request.WithContext(ctx)

In the router definition, use the middleware:


Define a function to recover the gin.Context from the context.Context struct:

func GinContextFromContext(ctx context.Context) (*gin.Context, error) {
	ginContext := ctx.Value("GinContextKey")
	if ginContext == nil {
		err := fmt.Errorf("could not retrieve gin.Context")
		return nil, err

	gc, ok := ginContext.(*gin.Context)
	if !ok {
		err := fmt.Errorf("gin.Context has wrong type")
		return nil, err
	return gc, nil

Lastly, in the Resolver, retrieve the gin.Context with the previous defined function:

func (r *resolver) Todo(ctx context.Context) (*Todo, error) {
	gc, err := GinContextFromContext(ctx)
	if err != nil {
		return nil, err

	// ...