From 275f6fecd865d95d0aceb8809dd5e1f1ab5bbbfe Mon Sep 17 00:00:00 2001 From: vvakame Date: Thu, 28 Nov 2019 16:24:34 +0900 Subject: [PATCH] always return OperationContext for postpone process --- graphql/context_operation.go | 2 +- graphql/executable_schema_mock.go | 3 ++- graphql/handler/apollotracing/tracer_test.go | 20 ++++++++++++++++++++ graphql/handler/executor.go | 8 ++++---- 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/graphql/context_operation.go b/graphql/context_operation.go index 2265a82b8c4..cf1a279ec7b 100644 --- a/graphql/context_operation.go +++ b/graphql/context_operation.go @@ -52,7 +52,7 @@ func GetRequestContext(ctx context.Context) *RequestContext { } func GetOperationContext(ctx context.Context) *OperationContext { - if val, ok := ctx.Value(operationCtx).(*OperationContext); ok { + if val, ok := ctx.Value(operationCtx).(*OperationContext); ok && val != nil { return val } panic("missing operation context") diff --git a/graphql/executable_schema_mock.go b/graphql/executable_schema_mock.go index c099de793e9..86eaf8334c8 100644 --- a/graphql/executable_schema_mock.go +++ b/graphql/executable_schema_mock.go @@ -5,8 +5,9 @@ package graphql import ( "context" - "github.com/vektah/gqlparser/ast" "sync" + + "github.com/vektah/gqlparser/ast" ) var ( diff --git a/graphql/handler/apollotracing/tracer_test.go b/graphql/handler/apollotracing/tracer_test.go index a905e7ffdd2..f94070c33d2 100644 --- a/graphql/handler/apollotracing/tracer_test.go +++ b/graphql/handler/apollotracing/tracer_test.go @@ -8,10 +8,13 @@ import ( "testing" "github.com/99designs/gqlgen/graphql/handler/apollotracing" + "github.com/99designs/gqlgen/graphql/handler/extension" + "github.com/99designs/gqlgen/graphql/handler/lru" "github.com/99designs/gqlgen/graphql/handler/testserver" "github.com/99designs/gqlgen/graphql/handler/transport" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "github.com/vektah/gqlparser/gqlerror" ) func TestApolloTracing(t *testing.T) { @@ -48,7 +51,24 @@ func TestApolloTracing(t *testing.T) { require.EqualValues(t, "Query", tracing.Execution.Resolvers[0].ParentType) require.EqualValues(t, "name", tracing.Execution.Resolvers[0].FieldName) require.EqualValues(t, "String!", tracing.Execution.Resolvers[0].ReturnType) +} +func TestApolloTracing_withFail(t *testing.T) { + h := testserver.New() + h.AddTransport(transport.POST{}) + h.Use(extension.AutomaticPersistedQuery{Cache: lru.New(100)}) + h.Use(apollotracing.Tracer{}) + + resp := doRequest(h, "POST", "/graphql", `{"operationName":"A","extensions":{"persistedQuery":{"version":1,"sha256Hash":"338bbc16ac780daf81845339fbf0342061c1e9d2b702c96d3958a13a557083a6"}}}`) + assert.Equal(t, http.StatusUnprocessableEntity, resp.Code, resp.Body.String()) + b := resp.Body.Bytes() + t.Log(string(b)) + var respData struct { + Errors gqlerror.List + } + require.NoError(t, json.Unmarshal(b, &respData)) + require.Equal(t, 1, len(respData.Errors)) + require.Equal(t, "PersistedQueryNotFound", respData.Errors[0].Message) } func doRequest(handler http.Handler, method string, target string, body string) *httptest.ResponseRecorder { diff --git a/graphql/handler/executor.go b/graphql/handler/executor.go index 43643a672b4..e3b751a2f38 100644 --- a/graphql/handler/executor.go +++ b/graphql/handler/executor.go @@ -126,7 +126,7 @@ func (e executor) CreateOperationContext(ctx context.Context, params *graphql.Ra for _, p := range e.operationParameterMutators { if err := p.MutateOperationParameters(ctx, params); err != nil { - return nil, gqlerror.List{err} + return rc, gqlerror.List{err} } } @@ -136,18 +136,18 @@ func (e executor) CreateOperationContext(ctx context.Context, params *graphql.Ra var listErr gqlerror.List rc.Doc, listErr = e.parseQuery(ctx, &rc.Stats, params.Query) if len(listErr) != 0 { - return nil, listErr + return rc, listErr } rc.Operation = rc.Doc.Operations.ForName(params.OperationName) if rc.Operation == nil { - return nil, gqlerror.List{gqlerror.Errorf("operation %s not found", params.OperationName)} + return rc, gqlerror.List{gqlerror.Errorf("operation %s not found", params.OperationName)} } var err *gqlerror.Error rc.Variables, err = validator.VariableValues(e.server.es.Schema(), rc.Operation, params.Variables) if err != nil { - return nil, gqlerror.List{err} + return rc, gqlerror.List{err} } rc.Stats.Validation.End = graphql.Now()