This repository has been archived on 2022-09-04. You can view files and clone it, but cannot push or open issues or pull requests.
api/graphql/delivery/http/handler.go

43 lines
1.1 KiB
Go
Raw Normal View History

2020-06-02 15:45:21 +00:00
package httpdelivery
import (
"fmt"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/gin-gonic/gin"
"github.com/tribalwarshelp/api/graphql/generated"
"github.com/tribalwarshelp/api/graphql/resolvers"
)
func Attach(g *gin.RouterGroup, r *resolvers.Resolver) error {
if r == nil {
return fmt.Errorf("Graphql resolver cannot be nil")
}
g.POST("/graphql", graphqlHandler(r))
g.GET("/", playgroundHandler())
return nil
}
// Defining the Graphql handler
func graphqlHandler(r *resolvers.Resolver) gin.HandlerFunc {
// NewExecutableSchema and Config are in the generated.go file
// Resolver is in the resolver.go file
cfg := generated.Config{Resolvers: r}
h := handler.NewDefaultServer(generated.NewExecutableSchema(cfg))
return func(c *gin.Context) {
c.Header("Cache-Control", "no-store, must-revalidate")
h.ServeHTTP(c.Writer, c.Request)
}
}
// Defining the Playground handler
func playgroundHandler() gin.HandlerFunc {
h := playground.Handler("Playground", "/graphql")
return func(c *gin.Context) {
h.ServeHTTP(c.Writer, c.Request)
}
}