dtpa98038 2017-12-16 08:31
浏览 152

将echo.Context转换为context.Context

I am writing a web application, using Go as the backend. I'm using this GraphQL library (link), and the Echo web framework (link). The problem is that that the graphql-go library uses the context package in Go, while Echo uses its own custom context, and removed support for the standard context package.

My question would be, is there a way to use context.Context as echo.Context, in the following example?

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

How would I make the echo context available to my graphql resolvers. Thank you very much, any help is appreciated.

  • 写回答

1条回答 默认 最新

  • douxin8610 2017-12-16 09:07
    关注

    An echo.Context can't be used as a context.Context, but it does refer to one; if c is the echo context then c.Request().Context() is the context.Context for the incoming request, which will be canceled if the user closes the connection, for instance.

    You can copy other useful values from the echo context to the stdlib context, if needed, by using the Get method on the one hand and the WithValue method on the other. If there are some values that you always want copied, then you can write a helper function to do so.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起