尝试将 Gqlgen 与 julienschmidt/httprouter 一起使用时找不到 404 页面
404 page not found when trying to use Gqlgen with julienschmidt/httprouter
下面是我的代码运行
问题是我找不到 404 页面而不是 graphql playground 页面
是否可以使用带有 gqlgen 的 httprouter 还是我需要返回到 chi 或 mux
我也无法使用中间件,因为 r 没有 Use 方法
package main
import (
"gographql-server/graph"
"gographql-server/graph/generated"
"log"
"net/http"
"os"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/julienschmidt/httprouter"
)
const defaultPort = "8080"
func PlaygroundHandler() httprouter.Handle {
h := playground.Handler("GraphQL", "/query")
return func(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
h.ServeHTTP(w, req)
}
}
func GraphqlHandler() httprouter.Handle {
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() {
port := os.Getenv("PORT")
if port == "" {
port = defaultPort
}
r := httprouter.New()
r.POST("/query", GraphqlHandler())
r.GET("/", PlaygroundHandler())
// r.Use(middleware.RequestID)
// r.Use(middleware.Logger)
// r.Use(middleware.Recoverer)
// r.Use(middlewares.AuthMiddleware())
log.Printf("connect to http://localhost:%s/ for GraphQL playground", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}
如果您在这一行中将 nil
传递给 http.ListenAndServe()
:
log.Fatal(http.ListenAndServe(":"+port, nil))
这意味着 DefaultServeMux
of the net/http
package is used. But you you don't register any handlers in the default mux, you create your own router using httprouter.New()
,您可以在其中注册处理程序。
所以你必须把这个路由器传给ListenAndServe()
:
log.Fatal(http.ListenAndServe(":"+port, r))
下面是我的代码运行 问题是我找不到 404 页面而不是 graphql playground 页面 是否可以使用带有 gqlgen 的 httprouter 还是我需要返回到 chi 或 mux 我也无法使用中间件,因为 r 没有 Use 方法
package main
import (
"gographql-server/graph"
"gographql-server/graph/generated"
"log"
"net/http"
"os"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/julienschmidt/httprouter"
)
const defaultPort = "8080"
func PlaygroundHandler() httprouter.Handle {
h := playground.Handler("GraphQL", "/query")
return func(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
h.ServeHTTP(w, req)
}
}
func GraphqlHandler() httprouter.Handle {
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() {
port := os.Getenv("PORT")
if port == "" {
port = defaultPort
}
r := httprouter.New()
r.POST("/query", GraphqlHandler())
r.GET("/", PlaygroundHandler())
// r.Use(middleware.RequestID)
// r.Use(middleware.Logger)
// r.Use(middleware.Recoverer)
// r.Use(middlewares.AuthMiddleware())
log.Printf("connect to http://localhost:%s/ for GraphQL playground", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}
如果您在这一行中将 nil
传递给 http.ListenAndServe()
:
log.Fatal(http.ListenAndServe(":"+port, nil))
这意味着 DefaultServeMux
of the net/http
package is used. But you you don't register any handlers in the default mux, you create your own router using httprouter.New()
,您可以在其中注册处理程序。
所以你必须把这个路由器传给ListenAndServe()
:
log.Fatal(http.ListenAndServe(":"+port, r))