git.sr.ht/~sircmpwn/gqlgen@v0.0.0-20200522192042-c84d29a1c940/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  ```bash
    16  $ go get github.com/gin-gonic/gin
    17  ```
    18  
    19  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:
    20  
    21  ```go
    22  import (
    23  	"github.com/gin-gonic/gin"
    24  
    25  	"git.sr.ht/~sircmpwn/gqlgen/graphql/handler"
    26  	"git.sr.ht/~sircmpwn/gqlgen/graphql/playground"
    27  )
    28  
    29  // Defining the Graphql handler
    30  func graphqlHandler() gin.HandlerFunc {
    31  	// NewExecutableSchema and Config are in the generated.go file
    32  	// Resolver is in the resolver.go file
    33  	h := handler.NewDefaultServer(NewExecutableSchema(Config{Resolvers: &Resolver{}}))
    34  
    35  	return func(c *gin.Context) {
    36  		h.ServeHTTP(c.Writer, c.Request)
    37  	}
    38  }
    39  
    40  // Defining the Playground handler
    41  func playgroundHandler() gin.HandlerFunc {
    42  	h := playground.Handler("GraphQL", "/query")
    43  
    44  	return func(c *gin.Context) {
    45  		h.ServeHTTP(c.Writer, c.Request)
    46  	}
    47  }
    48  
    49  func main() {
    50  	// Setting up Gin
    51  	r := gin.Default()
    52  	r.POST("/query", graphqlHandler())
    53  	r.GET("/", playgroundHandler())
    54  	r.Run()
    55  }
    56  
    57  ```
    58  
    59  ## Accessing gin.Context
    60  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.
    61  
    62  First, create a `gin` middleware to add its context to the `context.Context`:
    63  ```go
    64  func GinContextToContextMiddleware() gin.HandlerFunc {
    65  	return func(c *gin.Context) {
    66  		ctx := context.WithValue(c.Request.Context(), "GinContextKey", c)
    67  		c.Request = c.Request.WithContext(ctx)
    68  		c.Next()
    69  	}
    70  }
    71  ```
    72  
    73  In the router definition, use the middleware:
    74  ```go
    75  r.Use(GinContextToContextMiddleware())
    76  ```
    77  
    78  Define a function to recover the `gin.Context` from the `context.Context` struct:
    79  ```go
    80  func GinContextFromContext(ctx context.Context) (*gin.Context, error) {
    81  	ginContext := ctx.Value("GinContextKey")
    82  	if ginContext == nil {
    83  		err := fmt.Errorf("could not retrieve gin.Context")
    84  		return nil, err
    85  	}
    86  
    87  	gc, ok := ginContext.(*gin.Context)
    88  	if !ok {
    89  		err := fmt.Errorf("gin.Context has wrong type")
    90  		return nil, err
    91  	}
    92  	return gc, nil
    93  }
    94  ```
    95  
    96  Lastly, in the Resolver, retrieve the `gin.Context` with the previous defined function:
    97  ```go
    98  func (r *resolver) Todo(ctx context.Context) (*Todo, error) {
    99  	gc, err := GinContextFromContext(ctx)
   100  	if err != nil {
   101  		return nil, err
   102  	}
   103  
   104  	// ...
   105  }
   106  ```