使用 go-chi 路由器时无法连接到 graphql playground
Cannot connect to graphql playground when using go-chi router
我正在使用 gqlgen 创建一个 go graphql 服务器。
在教程中,localhost:8080
的默认设置工作正常。
server.go
...
func main() {
srv := handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: &graph.Resolver{}}))
http.Handle("/", playground.Handler("GraphQL playground", "/query"))
http.Handle("/query", srv)
log.Fatal(http.ListenAndServe(":8080", nil))
}
...
但是,如果我切换到 Chi 包路由器,我会收到 400 错误:
server.go
...
func main() {
router := chi.NewRouter()
srv := handler.New(generated.NewExecutableSchema(generated.Config{Resolvers: &graph.Resolver{}}))
router.Handle("/", playground.Handler("GraphQL Playground", "/query"))
router.Handle("/query", srv)
err := http.ListenAndServe(":8080", router)
if err != nil {
panic(err)
}
}
...
原来你必须使用 NewDefaultServer
而不是 New
:
srv := handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: &graph.Resolver{}}))
我正在使用 gqlgen 创建一个 go graphql 服务器。
在教程中,localhost:8080
的默认设置工作正常。
server.go
...
func main() {
srv := handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: &graph.Resolver{}}))
http.Handle("/", playground.Handler("GraphQL playground", "/query"))
http.Handle("/query", srv)
log.Fatal(http.ListenAndServe(":8080", nil))
}
...
但是,如果我切换到 Chi 包路由器,我会收到 400 错误:
server.go
...
func main() {
router := chi.NewRouter()
srv := handler.New(generated.NewExecutableSchema(generated.Config{Resolvers: &graph.Resolver{}}))
router.Handle("/", playground.Handler("GraphQL Playground", "/query"))
router.Handle("/query", srv)
err := http.ListenAndServe(":8080", router)
if err != nil {
panic(err)
}
}
...
原来你必须使用 NewDefaultServer
而不是 New
:
srv := handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: &graph.Resolver{}}))