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