Graphql-go: Using this library with Labstack's Echo framework

Created on 16 Dec 2017  Â·  7Comments  Â·  Source: graph-gophers/graphql-go

Hi guys,

I am using the echo framework which uses a custom context (echo.Context) which doesn't seem to be compatible with the standard context.Context library.

I have a JWT middleware like so:

func (api *API) Bind(group *echo.Group) {
    group.Use(middleware.JWTWithConfig(middleware.JWTConfig{
        SigningKey:     []byte("SOME_REAL_KEY"),
        SigningMethod:  "HS256",
    }))
    group.GET("/graphql", api.GraphQLHandler)
    group.POST("/query",  echo.WrapHandler(&relay.Handler{Schema: schema}))
}

and a resolver function like so:

func (r *Resolver) Viewer(ctx context.Context, arg *struct{
    Token *string
}) (*viewerResolver, error) {
    token := ctx.Value("user").(*jwt.Token)
    ...
}

that line fails because ctx.Value("user") returns nil, since it is not in the standard context, and is in echo.Context.

How would I be able to get the values from echo.Context in my resolvers.

Thank you so much!

Most helpful comment

A custom warp handler.

func warpHandler(h http.Handler) echo.HandlerFunc {
    return func(c echo.Context) error {
        oriReq := c.Request()
        oriCtx := oriReq.Context()
        req := oriReq.WithContext(context.WithValue(oriCtx, "echo_ctx", c))
        h.ServeHTTP(c.Response(), req)
        return nil
    }
}

And I don't recommend using this library with echo framework.

All 7 comments

A custom warp handler.

func warpHandler(h http.Handler) echo.HandlerFunc {
    return func(c echo.Context) error {
        oriReq := c.Request()
        oriCtx := oriReq.Context()
        req := oriReq.WithContext(context.WithValue(oriCtx, "echo_ctx", c))
        h.ServeHTTP(c.Response(), req)
        return nil
    }
}

And I don't recommend using this library with echo framework.

@sanae10001 ok, thank you :) What would you recommend using with this library, if anything. For example, buffalo (https://gobuffalo.io/)? Or just building from scratch, which I'm not entirely sure I want to do.

Thanks again

@sikula
For me.I use standard http library net/http and negroni .
The full web framework is heavy for a pure Graphql API server.

@sanae10001 So my stack is ReactJS for the frontned, Go for the backend with a Postgres database and GraphQL. The server would essentially just serve the static files. It would seem that you're suggesting that using a framework would be too much, and should just use standard library + negroni?

Sorry if the question seems basic, just started picking up Go and do not fully know the best practices yet. Thank you for the help though. I appreciate it.

@sikula

It would seem that you're suggesting that using a framework would be too much, and should just use standard library + negroni?

Yes. Using standard library can better help you learning how to process http in go.
For my experience, the solution what standard library contain always is best practices in go.

And recommend close this issue if you have no other problems.

You can try to encapsulate the echo version of graphql-go/relay。

Call graphql.MustParseSchema(s, &query{}) to generate *Schema, then parse it

exmaple:

GqlResponse(ctx, graphql.MustParseSchema(s, &query{}))

func GqlResponse(ctx echo.Context, Schema *graphql.Schema) error {

    r := ctx.Request()

    var params struct {
        Query         string                 `json:"query"`
        OperationName string                 `json:"operationName"`
        Variables     map[string]interface{} `json:"variables"`
    }

    if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
    }


    response := Schema.Exec(r.Context(), params.Query, params.OperationName, params.Variables)
    responseJSON, err := json.Marshal(response)
    if err != nil {
    }

    ctx.Response().Write(responseJSON)

    return nil
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Hagbarth picture Hagbarth  Â·  6Comments

jakubdal picture jakubdal  Â·  6Comments

zeitxx picture zeitxx  Â·  5Comments

CreatCodeBuild picture CreatCodeBuild  Â·  4Comments

thewillhuang picture thewillhuang  Â·  5Comments