0

whenever I change http with router I can't access my graphql playground when i use router instead of http with i get 404

func main() {
    port := os.Getenv("PORT")
    if port == "" {
        port = defaultPort
    }
    router := chi.NewRouter()
    srv := handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: &graph.Resolver{}}))

    // http.Handle("/", playground.Handler("GraphQL playground", "/query"))
    // http.Handle("/query", srv)
    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(":"+port, nil))
}

localhost:8080

  • You need to pass the `router` to `ListenAndServe`, e.g. `log.Fatal(http.ListenAndServe(":"+port, router))`. When you pass in `nil` then `ListenAndServe` will use the [`net/http.DefaultServeMux`](https://golang.org/pkg/net/http/#DefaultServeMux). – mkopriva Mar 25 '21 at 18:34
  • 1
    thank you, everything works fine now – Amir Haouala Mar 25 '21 at 23:34

0 Answers0