diff --git a/graphql/context_test.go b/graphql/context_test.go index 14304bcd3b3..85387e930df 100644 --- a/graphql/context_test.go +++ b/graphql/context_test.go @@ -8,6 +8,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/vektah/gqlparser/ast" + "github.com/vektah/gqlparser/gqlerror" ) func TestRequestContext_GetErrors(t *testing.T) { @@ -33,8 +34,21 @@ func TestRequestContext_GetErrors(t *testing.T) { Parent: root, Index: &index, } + userProvidedPath := &ResolverContext{ + Parent: child, + Field: CollectedField{ + Field: &ast.Field{ + Alias: "works", + }, + }, + } + ctx = WithResolverContext(ctx, child) c.Error(ctx, errors.New("bar")) + c.Error(ctx, &gqlerror.Error{ + Message: "foo3", + Path: append(child.Path(), "works"), + }) specs := []struct { Name string @@ -51,6 +65,11 @@ func TestRequestContext_GetErrors(t *testing.T) { RCtx: child, Messages: []string{"bar"}, }, + { + Name: "with user provided path", + RCtx: userProvidedPath, + Messages: []string{"foo3"}, + }, } for _, spec := range specs { diff --git a/graphql/error.go b/graphql/error.go index 7f161a4306b..af8b4ce4088 100644 --- a/graphql/error.go +++ b/graphql/error.go @@ -14,7 +14,9 @@ type ExtendedError interface { func DefaultErrorPresenter(ctx context.Context, err error) *gqlerror.Error { if gqlerr, ok := err.(*gqlerror.Error); ok { - gqlerr.Path = GetResolverContext(ctx).Path() + if gqlerr.Path == nil { + gqlerr.Path = GetResolverContext(ctx).Path() + } return gqlerr }