我有一个我一直在编写的 GraphQL API,并且想知道当您已经在使用传递数据源go时如何管理 JWT 身份验证。context
main所以我的函数的缩写版本是:
import (
"net/http"
"github.com/graphql-go/graphql"
gqlhandler "github.com/graphql-go/handler"
)
func queryHandler(ds *sources.DataSources, gql *gqlhandler.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := context.WithValue(context.Background(), sources.CtxSourcesKey, ds)
gql.ContextHandler(ctx, w, r)
})
}
func main() {
apiSchema, _ := schema.CompileSchema(schema.QueryType, schema.MutationType)
gql := gqlhandler.New(&gqlhandler.Config{
Schema: &apiSchema,
GraphiQL: !isDeployed,
Pretty: false,
Playground: false,
})
http.ListenAndServe(":41000", util.CreateChiRouter(healthCheckHandler(), queryHandler(ctxSources, gql)))
}
如您所见,我已经创建了一个新context实例来存储我的各种数据源的映射并将其传递给查询解析函数,但还需要能够解析出Authorization可能的 JWT 的标头以传递给认证的路由。
鉴于我目前的情况,最好的方法是什么?(将 JWT 与数据源上下文相结合?以不同方式处理数据源以释放context?)
慕田峪7331174
相关分类