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