From 652aa2fb2917fc4c2362abc608e7643ee589daa7 Mon Sep 17 00:00:00 2001 From: vvakame Date: Thu, 6 Feb 2020 17:52:23 +0900 Subject: [PATCH] propagate errors to response context in DispatchError --- graphql/handler/executor.go | 4 ++++ graphql/handler/server_test.go | 17 +++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/graphql/handler/executor.go b/graphql/handler/executor.go index eac1537a5d6..9bc0b2ab409 100644 --- a/graphql/handler/executor.go +++ b/graphql/handler/executor.go @@ -167,6 +167,10 @@ func (e executor) CreateOperationContext(ctx context.Context, params *graphql.Ra func (e executor) DispatchError(ctx context.Context, list gqlerror.List) *graphql.Response { ctx = graphql.WithResponseContext(ctx, e.server.errorPresenter, e.server.recoverFunc) + for _, gErr := range list { + graphql.AddError(ctx, gErr) + } + resp := e.responseMiddleware(ctx, func(ctx context.Context) *graphql.Response { resp := &graphql.Response{ Errors: list, diff --git a/graphql/handler/server_test.go b/graphql/handler/server_test.go index 630e9a845b7..a431a14288d 100644 --- a/graphql/handler/server_test.go +++ b/graphql/handler/server_test.go @@ -13,6 +13,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/vektah/gqlparser/ast" + "github.com/vektah/gqlparser/gqlerror" "github.com/vektah/gqlparser/parser" ) @@ -92,6 +93,22 @@ func TestServer(t *testing.T) { assert.Equal(t, []string{"first", "second"}, calls) }) + t.Run("get query parse error in AroundResponses", func(t *testing.T) { + var errors1 gqlerror.List + var errors2 gqlerror.List + srv.AroundResponses(func(ctx context.Context, next graphql.ResponseHandler) *graphql.Response { + resp := next(ctx) + errors1 = graphql.GetErrors(ctx) + errors2 = resp.Errors + return resp + }) + + resp := get(srv, "/foo?query=invalid") + assert.Equal(t, http.StatusUnprocessableEntity, resp.Code, resp.Body.String()) + assert.Equal(t, 1, len(errors1)) + assert.Equal(t, 1, len(errors2)) + }) + t.Run("query caching", func(t *testing.T) { ctx := context.Background() cache := &graphql.MapCache{}