2
1

More than 3 years have passed since last update.

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())
    ...
}
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1