Deprecation Fix: Updated gqlgen server handler initialization (#1415)

Only changed to the new initialization API.
To minimize risk of possible regressions, I tried to make as little changes as possible.

Resolves #1135
This commit is contained in:
EnameEtavir
2021-05-31 05:58:32 +02:00
committed by GitHub
parent 3e81d45ae9
commit b5a26cec8b

View File

@@ -14,7 +14,11 @@ import (
"strings" "strings"
"time" "time"
"github.com/99designs/gqlgen/handler" gqlHandler "github.com/99designs/gqlgen/graphql/handler"
gqlExtension "github.com/99designs/gqlgen/graphql/handler/extension"
gqlLru "github.com/99designs/gqlgen/graphql/handler/lru"
gqlTransport "github.com/99designs/gqlgen/graphql/handler/transport"
gqlPlayground "github.com/99designs/gqlgen/graphql/playground"
"github.com/go-chi/chi" "github.com/go-chi/chi"
"github.com/go-chi/chi/middleware" "github.com/go-chi/chi/middleware"
"github.com/gobuffalo/packr/v2" "github.com/gobuffalo/packr/v2"
@@ -80,7 +84,10 @@ func authenticateHandler() func(http.Handler) http.Handler {
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error())) _, err = w.Write([]byte(err.Error()))
if err != nil {
logger.Error(err)
}
return return
} }
@@ -139,33 +146,48 @@ func Start() {
r.Use(cors.AllowAll().Handler) r.Use(cors.AllowAll().Handler)
r.Use(BaseURLMiddleware) r.Use(BaseURLMiddleware)
recoverFunc := handler.RecoverFunc(func(ctx context.Context, err interface{}) error { recoverFunc := func(ctx context.Context, err interface{}) error {
logger.Error(err) logger.Error(err)
debug.PrintStack() debug.PrintStack()
message := fmt.Sprintf("Internal system error. Error <%v>", err) message := fmt.Sprintf("Internal system error. Error <%v>", err)
return errors.New(message) return errors.New(message)
}) }
websocketUpgrader := handler.WebsocketUpgrader(websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
})
maxUploadSize := handler.UploadMaxSize(c.GetMaxUploadSize())
websocketKeepAliveDuration := handler.WebsocketKeepAliveDuration(10 * time.Second)
txnManager := manager.GetInstance().TxnManager txnManager := manager.GetInstance().TxnManager
resolver := &Resolver{ resolver := &Resolver{
txnManager: txnManager, txnManager: txnManager,
} }
gqlHandler := handler.GraphQL(models.NewExecutableSchema(models.Config{Resolvers: resolver}), recoverFunc, websocketUpgrader, websocketKeepAliveDuration, maxUploadSize) gqlSrv := gqlHandler.New(models.NewExecutableSchema(models.Config{Resolvers: resolver}))
gqlSrv.SetRecoverFunc(recoverFunc)
gqlSrv.AddTransport(gqlTransport.Websocket{
Upgrader: websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
},
KeepAlivePingInterval: 10 * time.Second,
})
gqlSrv.AddTransport(gqlTransport.Options{})
gqlSrv.AddTransport(gqlTransport.GET{})
gqlSrv.AddTransport(gqlTransport.POST{})
gqlSrv.AddTransport(gqlTransport.MultipartForm{
MaxUploadSize: c.GetMaxUploadSize(),
})
gqlSrv.SetQueryCache(gqlLru.New(1000))
gqlSrv.Use(gqlExtension.Introspection{})
gqlHandlerFunc := func(w http.ResponseWriter, r *http.Request) {
gqlSrv.ServeHTTP(w, r)
}
// register GQL handler with plugin cache // register GQL handler with plugin cache
manager.GetInstance().PluginCache.RegisterGQLHandler(gqlHandler) manager.GetInstance().PluginCache.RegisterGQLHandler(gqlHandlerFunc)
r.Handle("/graphql", gqlHandler) r.HandleFunc("/graphql", gqlHandlerFunc)
r.Handle("/playground", handler.Playground("GraphQL playground", "/graphql")) r.HandleFunc("/playground", gqlPlayground.Handler("GraphQL playground", "/graphql"))
// session handlers // session handlers
r.Post(loginEndPoint, handleLogin) r.Post(loginEndPoint, handleLogin)