github.com/operandinc/gqlgen@v0.16.1/docs/content/recipes/gin.md (about)

     1  ---
     2  title: "Using Gin to setup HTTP handlers"
     3  description: Setting up HTTP handlers using Gin, a HTTP web framework written in Go.
     4  linkTitle: Gin
     5  menu: { main: { parent: "recipes" } }
     6  ---
     7  
     8  Gin is an excellent alternative for the `net/http` router. From their official [GitHub page](https://github.com/gin-gonic/gin):
     9  
    10  > 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.
    11  
    12  Here are the steps to setup Gin and gqlgen together:
    13  
    14  Install Gin:
    15  
    16  ```bash
    17  $ go get github.com/gin-gonic/gin
    18  ```
    19  
    20  In your router file, define the handlers for the GraphQL and Playground endpoints in two different methods and tie them together in the Gin router:
    21  
    22  ```go
    23  import (
    24  	"github.com/[username]/gqlgen-todos/graph"	// Replace username with your github username
    25  	"github.com/[username]/gqlgen-todos/graph/generated" // Replace username with your github username
    26  	"github.com/gin-gonic/gin"
    27  
    28  	"github.com/operandinc/gqlgen/graphql/handler"
    29  	"github.com/operandinc/gqlgen/graphql/playground"
    30  )
    31  
    32  // Defining the Graphql handler
    33  func graphqlHandler() gin.HandlerFunc {
    34  	// NewExecutableSchema and Config are in the generated.go file
    35  	// Resolver is in the resolver.go file
    36  	h := handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: &graph.Resolver{}}))
    37  
    38  	return func(c *gin.Context) {
    39  		h.ServeHTTP(c.Writer, c.Request)
    40  	}
    41  }
    42  
    43  // Defining the Playground handler
    44  func playgroundHandler() gin.HandlerFunc {
    45  	h := playground.Handler("GraphQL", "/query")
    46  
    47  	return func(c *gin.Context) {
    48  		h.ServeHTTP(c.Writer, c.Request)
    49  	}
    50  }
    51  
    52  func main() {
    53  	// Setting up Gin
    54  	r := gin.Default()
    55  	r.POST("/query", graphqlHandler())
    56  	r.GET("/", playgroundHandler())
    57  	r.Run()
    58  }
    59  
    60  ```
    61  
    62  ## Accessing gin.Context
    63  
    64  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.
    65  
    66  First, create a `gin` middleware to add its context to the `context.Context`:
    67  
    68  ```go
    69  func GinContextToContextMiddleware() gin.HandlerFunc {
    70  	return func(c *gin.Context) {
    71  		ctx := context.WithValue(c.Request.Context(), "GinContextKey", c)
    72  		c.Request = c.Request.WithContext(ctx)
    73  		c.Next()
    74  	}
    75  }
    76  ```
    77  
    78  In the router definition, use the middleware:
    79  
    80  ```go
    81  r.Use(GinContextToContextMiddleware())
    82  ```
    83  
    84  Define a function to recover the `gin.Context` from the `context.Context` struct:
    85  
    86  ```go
    87  func GinContextFromContext(ctx context.Context) (*gin.Context, error) {
    88  	ginContext := ctx.Value("GinContextKey")
    89  	if ginContext == nil {
    90  		err := fmt.Errorf("could not retrieve gin.Context")
    91  		return nil, err
    92  	}
    93  
    94  	gc, ok := ginContext.(*gin.Context)
    95  	if !ok {
    96  		err := fmt.Errorf("gin.Context has wrong type")
    97  		return nil, err
    98  	}
    99  	return gc, nil
   100  }
   101  ```
   102  
   103  Lastly, in the Resolver, retrieve the `gin.Context` with the previous defined function:
   104  
   105  ```go
   106  func (r *resolver) Todo(ctx context.Context) (*Todo, error) {
   107  	gc, err := GinContextFromContext(ctx)
   108  	if err != nil {
   109  		return nil, err
   110  	}
   111  
   112  	// ...
   113  }
   114  ```