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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With