package main import ( "context" "github.com/99designs/gqlgen/graphql/handler" "github.com/99designs/gqlgen/graphql/playground" "github.com/gin-gonic/gin" "github.com/yauthdev/yauth/server/graph" "github.com/yauthdev/yauth/server/graph/generated" ) // 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(generated.NewExecutableSchema(generated.Config{Resolvers: &graph.Resolver{}})) return func(c *gin.Context) { h.ServeHTTP(c.Writer, c.Request) } } // Defining the Playground handler func playgroundHandler() gin.HandlerFunc { h := playground.Handler("GraphQL", "/graphql") return func(c *gin.Context) { h.ServeHTTP(c.Writer, c.Request) } } func GinContextToContextMiddleware() gin.HandlerFunc { return func(c *gin.Context) { ctx := context.WithValue(c.Request.Context(), "GinContextKey", c) c.Request = c.Request.WithContext(ctx) c.Next() } } func main() { r := gin.Default() r.Use(GinContextToContextMiddleware()) r.POST("/graphql", graphqlHandler()) r.GET("/", playgroundHandler()) r.Run() }