diff --git a/graphql/handler/apollofederatedtracingv1/tracing.go b/graphql/handler/apollofederatedtracingv1/tracing.go index 1c969b4f64..959ebbe38b 100644 --- a/graphql/handler/apollofederatedtracingv1/tracing.go +++ b/graphql/handler/apollofederatedtracingv1/tracing.go @@ -85,10 +85,12 @@ func (t *Tracer) InterceptResponse(ctx context.Context, next graphql.ResponseHan return next(ctx) } tb := t.getTreeBuilder(ctx) - if tb != nil { - tb.StartTimer(ctx) + if tb == nil { + return next(ctx) } + tb.StartTimer(ctx) + val := new(string) graphql.RegisterExtension(ctx, "ftv1", val) diff --git a/graphql/handler/apollofederatedtracingv1/tracing_test.go b/graphql/handler/apollofederatedtracingv1/tracing_test.go index aa05de7127..ee50cae8a5 100644 --- a/graphql/handler/apollofederatedtracingv1/tracing_test.go +++ b/graphql/handler/apollofederatedtracingv1/tracing_test.go @@ -110,6 +110,29 @@ func TestApolloTracing_withFail(t *testing.T) { require.Equal(t, "PersistedQueryNotFound", respData.Errors[0].Message) } +// This tests that the tracing extension does not panic when the request +// can't be processed for some reason. The specific cause is not +// important, the scenario being tested is the response interceptor +// being run to process the error response when no other interceptor +// has been run, due to (for example) a problem creating the OperationContext. +func TestApolloTracing_withMissingOp(t *testing.T) { + h := testserver.New() + h.AddTransport(transport.POST{}) + h.Use(extension.AutomaticPersistedQuery{Cache: lru.New(100)}) + h.Use(&apollofederatedtracingv1.Tracer{}) + + resp := doRequest(h, http.MethodPost, "/graphql", `{}`) + 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.Len(t, respData.Errors, 1) + require.Equal(t, "no operation provided", respData.Errors[0].Message) +} + func TestApolloTracing_withUnexpectedEOF(t *testing.T) { h := testserver.New() h.AddTransport(transport.POST{})