英文:
404 page not found when trying to use Gqlgen with julienschmidt/httprouter
问题
以下是我运行的代码。问题是我得到了404页面未找到,而不是graphql playground页面。是否可以使用httprouter与gqlgen一起工作,还是我需要回到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))
}
希望对你有帮助!
英文:
Below is the code I run
the issue is that I get 404 page not found instead of the graphql playground page
Is it possible to work with httprouter with gqlgen or do I need to go back to chi or mux
I also was not able to use middlewares because r does not have Use method
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))
}
答案1
得分: 1
如果你在这一行中将nil
传递给http.ListenAndServe()
:
log.Fatal(http.ListenAndServe(":"+port, nil))
这意味着使用了net/http
包中的DefaultServeMux
。但是如果你没有在默认的mux中注册任何处理程序,而是使用httprouter.New()
创建了自己的路由器,并在其中注册处理程序。
所以你必须将这个路由器传递给ListenAndServe()
:
log.Fatal(http.ListenAndServe(":"+port, r))
英文:
If you pass nil
to http.ListenAndServe()
in this line:
log.Fatal(http.ListenAndServe(":"+port, nil))
That means the 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()
, and you register handlers in that.
So you must pass this router to ListenAndServe()
:
log.Fatal(http.ListenAndServe(":"+port, r))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论