###gqlgenとhttprouteの組み方
import (
"net/http"
...
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/julienschmidt/httprouter"
)
// Defining the Playground handler
func PlaygroundHandler() httprouter.Handle {
h := playground.Handler("GraphQL", "/query")
return func(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
h.ServeHTTP(w, req)
}
}
// Defining the Graphql handler
func GraphqlHandler() httprouter.Handle {
// 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(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
h.ServeHTTP(w, req)
}
}
func main() {
// Setting up httprouter
r := httprouter.New()
r.POST("/query", GraphqlHandler())
r.GET("/", PlaygroundHandler())
...
}