我正在编写一个web应用程序,使用Go作为后端。我正在使用这个GraphQL库(链接)和Echo web框架(链接)。问题是graphql-go库在Go中使用context包,而Echo使用自己的自定义上下文,并删除了对标准context包的支持。
我的问题是,在下面的示例中,是否有一种将context.Context用作echo.Context的方法?
api.go
func (api *API) Bind(group *echo.Group) {
group.Use(middleware.JWTWithConfig(middleware.JWTConfig{
SigningKey: []byte("SOME_REAL_SECRET_KEY"),
SigningMethod: "HS256",
}))
group.GET("/graphql", api.GraphQLHandler)
group.POST("/query", echo.WrapHandler(&relay.Handler{Schema: schema}))
}graphql.go
func (r *Resolver) Viewer(ctx context.Context, arg *struct{ Token *string }) (*viewerResolver, error) {
token := ctx.Value("user").(*jwt.Token) // oops on this line, graphql-go uses context.Context, but the echo middleware provides echo.Context
...
}我将如何使回显上下文对我的graphql解析器可用。非常感谢您的帮助。
发布于 2017-12-16 09:07:54
echo.Context不能用作context.Context,但它确实引用了一个;如果c是回显上下文,那么c.Request().Context()是传入请求的context.Context,例如,如果用户关闭连接,那么c.Request().Context()将被取消。
如果需要,可以使用Get方法和WithValue方法将其他有用的值从回显上下文复制到stdlib上下文。如果您总是希望复制一些值,那么您可以编写一个帮助函数来这样做。
https://stackoverflow.com/questions/47843914
复制相似问题