0
package main

import (
    "github.com/go-chi/chi"
    "go-graphql-demo/graph"
    "go-graphql-demo/graph/generated"
    "log"
    "net/http"
    "os"

    "github.com/99designs/gqlgen/graphql/handler"
    "github.com/99designs/gqlgen/graphql/playground"
)



func main() {

    router := chi.NewRouter()

    srv := handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: &graph.Resolver{}}))

    router.Handle("/", playground.Handler("GraphQL playground", "/query"))
    router.Handle("/query", srv)

    log.Printf("connect to http://localhost:%s/ for GraphQL playground", port)
    log.Fatal(http.ListenAndServe(":8080", nil))
}

按此使用 handler.NewDefaultServer(...) 使用 go-chi 路由器时无法连接到 graphql Playground

参考 https://gqlgen.com/recipes/authentication/

4

1 回答 1

2

log.Fatal(http.ListenAndServe(":8080", nil)) 应该 log.Fatal(http.ListenAndServe(":8080", router))

看起来portvar 也没有设置。

于 2020-10-07T11:02:51.273 回答