2021-07-08 12:15:19 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-07-14 18:43:19 +00:00
|
|
|
"context"
|
2021-07-08 12:15:19 +00:00
|
|
|
|
|
|
|
"github.com/99designs/gqlgen/graphql/handler"
|
|
|
|
"github.com/99designs/gqlgen/graphql/playground"
|
2021-07-14 18:43:19 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2021-07-08 12:15:19 +00:00
|
|
|
"github.com/yauthdev/yauth/server/graph"
|
|
|
|
"github.com/yauthdev/yauth/server/graph/generated"
|
|
|
|
)
|
|
|
|
|
2021-07-14 18:43:19 +00:00
|
|
|
// 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{}}))
|
2021-07-08 12:15:19 +00:00
|
|
|
|
2021-07-14 18:43:19 +00:00
|
|
|
return func(c *gin.Context) {
|
|
|
|
h.ServeHTTP(c.Writer, c.Request)
|
2021-07-08 12:15:19 +00:00
|
|
|
}
|
2021-07-14 18:43:19 +00:00
|
|
|
}
|
2021-07-08 12:15:19 +00:00
|
|
|
|
2021-07-14 18:43:19 +00:00
|
|
|
// Defining the Playground handler
|
|
|
|
func playgroundHandler() gin.HandlerFunc {
|
|
|
|
h := playground.Handler("GraphQL", "/graphql")
|
2021-07-08 12:15:19 +00:00
|
|
|
|
2021-07-14 18:43:19 +00:00
|
|
|
return func(c *gin.Context) {
|
|
|
|
h.ServeHTTP(c.Writer, c.Request)
|
|
|
|
}
|
|
|
|
}
|
2021-07-08 12:15:19 +00:00
|
|
|
|
2021-07-14 18:43:19 +00:00
|
|
|
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
|
|
|
|
2021-07-14 18:43:19 +00:00
|
|
|
func main() {
|
|
|
|
r := gin.Default()
|
|
|
|
r.Use(GinContextToContextMiddleware())
|
|
|
|
r.POST("/graphql", graphqlHandler())
|
|
|
|
r.GET("/", playgroundHandler())
|
|
|
|
r.Run()
|
2021-07-08 12:15:19 +00:00
|
|
|
}
|