尝试使用Gqlgen与julienschmidt/httprouter时出现404页面未找到的错误。

huangapple go评论93阅读模式
英文:

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))

huangapple
  • 本文由 发表于 2021年8月9日 18:59:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/68710928.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定