From ee6dd6c2e8ab50d63af0511419aaa5b1895d83b6 Mon Sep 17 00:00:00 2001 From: Rajas Vanjape Date: Wed, 24 Mar 2021 14:13:02 +0530 Subject: [PATCH] Address Abhimanyu's comments --- graphql/resolve/resolver.go | 44 +++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/graphql/resolve/resolver.go b/graphql/resolve/resolver.go index 24c2473962b..f477d3a2be2 100644 --- a/graphql/resolve/resolver.go +++ b/graphql/resolve/resolver.go @@ -472,20 +472,6 @@ func (r *RequestResolver) Resolve(ctx context.Context, gqlReq *schema.Request) ( } startTime := time.Now() - ctx = context.WithValue(ctx, resolveStartTime, startTime) - - // Pass in GraphQL @auth information - ctx, err := r.schema.Meta().AuthMeta().AttachAuthorizationJwt(ctx, gqlReq.Header) - if err != nil { - return schema.ErrorResponse(err) - } - - ctx = x.AttachJWTNamespace(ctx) - op, err := r.schema.Operation(gqlReq) - if err != nil { - return schema.ErrorResponse(err) - } - resp = &schema.Response{ Extensions: &schema.Extensions{ Tracing: &schema.Trace{ @@ -495,10 +481,33 @@ func (r *RequestResolver) Resolve(ctx context.Context, gqlReq *schema.Request) ( }, } defer func() { + // Panic Handler for mutation. This ensures that the mutation which causes panic + // gets logged in Alpha logs. This panic handler overrides the default Panic Handler + // used in recoveryHandler in admin/http.go + defer api.PanicHandler( + func(err error) { + resp.Errors = schema.AsGQLErrors(schema.AppendGQLErrs(resp.Errors, err)) + }, gqlReq.Query) + endTime := time.Now() resp.Extensions.Tracing.EndTime = endTime.Format(time.RFC3339Nano) resp.Extensions.Tracing.Duration = endTime.Sub(startTime).Nanoseconds() }() + ctx = context.WithValue(ctx, resolveStartTime, startTime) + + // Pass in GraphQL @auth information + ctx, err := r.schema.Meta().AuthMeta().AttachAuthorizationJwt(ctx, gqlReq.Header) + if err != nil { + resp.Errors = schema.AsGQLErrors(err) + return + } + + ctx = x.AttachJWTNamespace(ctx) + op, err := r.schema.Operation(gqlReq) + if err != nil { + resp.Errors = schema.AsGQLErrors(err) + return + } if glog.V(3) { // don't log the introspection queries they are sent too frequently @@ -563,13 +572,6 @@ func (r *RequestResolver) Resolve(ctx context.Context, gqlReq *schema.Request) ( } resolveQueries() case op.IsMutation(): - // Panic Handler for mutation. This ensures that the mutation which causes panic - // gets logged in Alpha logs. This panic handler overrides the default Panic Handler - // used in recoveryHandler in admin/http.go - defer api.PanicHandler( - func(err error) { - resp.Errors = schema.AsGQLErrors(schema.AppendGQLErrs(resp.Errors, err)) - }, gqlReq.Query) // A mutation operation can contain any number of mutation fields. Those should be executed // serially. // (spec https://graphql.github.io/graphql-spec/June2018/#sec-Normal-and-Serial-Execution)