authorizer/server/server.go

48 lines
1.2 KiB
Go
Raw Normal View History

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()
}
}
2021-07-12 18:22:16 +00:00
func main() {
r := gin.Default()
r.Use(GinContextToContextMiddleware())
r.POST("/graphql", graphqlHandler())
r.GET("/", playgroundHandler())
r.Run()
}