简体   繁体   中英

Adding authentication middleware with go-chi router for GraphQL with gqlgen

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

Using handler.NewDefaultServer(...) as per this Cannot connect to graphql playground when using go-chi router

References https://gqlgen.com/recipes/authentication/

log.Fatal(http.ListenAndServe(":8080", nil)) should be log.Fatal(http.ListenAndServe(":8080", router))

Also looks like port var is not set.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM