diff --git a/codegen/generated!.gotpl b/codegen/generated!.gotpl index ed68c25adb8..03be286e1e1 100644 --- a/codegen/generated!.gotpl +++ b/codegen/generated!.gotpl @@ -10,7 +10,6 @@ {{ reserveImport "github.com/vektah/gqlparser/v2" }} {{ reserveImport "github.com/vektah/gqlparser/v2/ast" }} -{{ reserveImport "github.com/vektah/gqlparser/v2/gqlerror" }} {{ reserveImport "github.com/99designs/gqlgen/graphql" }} {{ reserveImport "github.com/99designs/gqlgen/graphql/introspection" }} diff --git a/codegen/testserver/directive_test.go b/codegen/testserver/directive_test.go index c201b255726..9175e18a40c 100644 --- a/codegen/testserver/directive_test.go +++ b/codegen/testserver/directive_test.go @@ -301,7 +301,7 @@ func TestDirectives(t *testing.T) { err := c.Post(`query { directiveInputNullable(arg: {text:"invalid text",inner:{message:"123"}}) }`, &resp) - require.EqualError(t, err, `[{"message":"not valid","path":["directiveInputNullable"]}]`) + require.EqualError(t, err, `[{"message":"not valid","path":["directiveInputNullable","arg"]}]`) require.Nil(t, resp.DirectiveInputNullable) }) t.Run("when function errors on inner directives", func(t *testing.T) { @@ -311,7 +311,7 @@ func TestDirectives(t *testing.T) { err := c.Post(`query { directiveInputNullable(arg: {text:"2",inner:{message:""}}) }`, &resp) - require.EqualError(t, err, `[{"message":"not valid","path":["directiveInputNullable"]}]`) + require.EqualError(t, err, `[{"message":"not valid","path":["directiveInputNullable","arg","inner"]}]`) require.Nil(t, resp.DirectiveInputNullable) }) t.Run("when function errors on nullable inner directives", func(t *testing.T) { @@ -321,7 +321,7 @@ func TestDirectives(t *testing.T) { err := c.Post(`query { directiveInputNullable(arg: {text:"success",inner:{message:"1"},innerNullable:{message:""}}) }`, &resp) - require.EqualError(t, err, `[{"message":"not valid","path":["directiveInputNullable"]}]`) + require.EqualError(t, err, `[{"message":"not valid","path":["directiveInputNullable","arg","innerNullable"]}]`) require.Nil(t, resp.DirectiveInputNullable) }) t.Run("when function success", func(t *testing.T) { diff --git a/codegen/testserver/generated.go b/codegen/testserver/generated.go index 5953c2e8a2c..933f4c5a8cf 100644 --- a/codegen/testserver/generated.go +++ b/codegen/testserver/generated.go @@ -19,7 +19,6 @@ import ( "github.com/99designs/gqlgen/graphql/introspection" gqlparser "github.com/vektah/gqlparser/v2" "github.com/vektah/gqlparser/v2/ast" - "github.com/vektah/gqlparser/v2/gqlerror" ) // region ************************** generated!.gotpl ************************** @@ -12349,7 +12348,8 @@ func (ec *executionContext) _iIt(ctx context.Context, sel ast.SelectionSet, obj // region ***************************** type.gotpl ***************************** func (ec *executionContext) unmarshalNBoolean2bool(ctx context.Context, v interface{}) (bool, error) { - return graphql.UnmarshalBoolean(v) + res, err := graphql.UnmarshalBoolean(v) + return res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) marshalNBoolean2bool(ctx context.Context, sel ast.SelectionSet, v bool) graphql.Marshaler { @@ -12363,7 +12363,8 @@ func (ec *executionContext) marshalNBoolean2bool(ctx context.Context, sel ast.Se } func (ec *executionContext) unmarshalNBytes2ᚕbyte(ctx context.Context, v interface{}) ([]byte, error) { - return UnmarshalBytes(v) + res, err := UnmarshalBytes(v) + return res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) marshalNBytes2ᚕbyte(ctx context.Context, sel ast.SelectionSet, v []byte) graphql.Marshaler { @@ -12391,7 +12392,8 @@ func (ec *executionContext) marshalNCheckIssue8962ᚖgithubᚗcomᚋ99designsᚋ } func (ec *executionContext) unmarshalNDefaultScalarImplementation2string(ctx context.Context, v interface{}) (string, error) { - return graphql.UnmarshalString(v) + res, err := graphql.UnmarshalString(v) + return res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) marshalNDefaultScalarImplementation2string(ctx context.Context, sel ast.SelectionSet, v string) graphql.Marshaler { @@ -12407,17 +12409,7 @@ func (ec *executionContext) marshalNDefaultScalarImplementation2string(ctx conte func (ec *executionContext) unmarshalNEmail2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐEmail(ctx context.Context, v interface{}) (Email, error) { var res Email err := res.UnmarshalGQL(v) - if err != nil { - fic := graphql.GetFieldInputContext(ctx) - path := fic.Path() - if gerr, ok := err.(*gqlerror.Error); ok { - gerr.Path = path - return res, gerr - } else { - return res, gqlerror.WrapPath(path, err) - } - } - return res, nil + return res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) marshalNEmail2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐEmail(ctx context.Context, sel ast.SelectionSet, v Email) graphql.Marshaler { @@ -12427,17 +12419,7 @@ func (ec *executionContext) marshalNEmail2githubᚗcomᚋ99designsᚋgqlgenᚋco func (ec *executionContext) unmarshalNEnumTest2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐEnumTest(ctx context.Context, v interface{}) (EnumTest, error) { var res EnumTest err := res.UnmarshalGQL(v) - if err != nil { - fic := graphql.GetFieldInputContext(ctx) - path := fic.Path() - if gerr, ok := err.(*gqlerror.Error); ok { - gerr.Path = path - return res, gerr - } else { - return res, gqlerror.WrapPath(path, err) - } - } - return res, nil + return res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) marshalNEnumTest2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐEnumTest(ctx context.Context, sel ast.SelectionSet, v EnumTest) graphql.Marshaler { @@ -12460,7 +12442,7 @@ func (ec *executionContext) marshalNError2ᚖgithubᚗcomᚋ99designsᚋgqlgen func (ec *executionContext) unmarshalNFallbackToStringEncoding2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐFallbackToStringEncoding(ctx context.Context, v interface{}) (FallbackToStringEncoding, error) { tmp, err := graphql.UnmarshalString(v) - return FallbackToStringEncoding(tmp), err + return FallbackToStringEncoding(tmp), graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) marshalNFallbackToStringEncoding2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐFallbackToStringEncoding(ctx context.Context, sel ast.SelectionSet, v FallbackToStringEncoding) graphql.Marshaler { @@ -12474,7 +12456,8 @@ func (ec *executionContext) marshalNFallbackToStringEncoding2githubᚗcomᚋ99de } func (ec *executionContext) unmarshalNID2int(ctx context.Context, v interface{}) (int, error) { - return graphql.UnmarshalIntID(v) + res, err := graphql.UnmarshalIntID(v) + return res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) marshalNID2int(ctx context.Context, sel ast.SelectionSet, v int) graphql.Marshaler { @@ -12488,7 +12471,8 @@ func (ec *executionContext) marshalNID2int(ctx context.Context, sel ast.Selectio } func (ec *executionContext) unmarshalNID2string(ctx context.Context, v interface{}) (string, error) { - return graphql.UnmarshalID(v) + res, err := graphql.UnmarshalID(v) + return res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) marshalNID2string(ctx context.Context, sel ast.SelectionSet, v string) graphql.Marshaler { @@ -12502,7 +12486,8 @@ func (ec *executionContext) marshalNID2string(ctx context.Context, sel ast.Selec } func (ec *executionContext) unmarshalNInnerDirectives2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐInnerDirectives(ctx context.Context, v interface{}) (InnerDirectives, error) { - return ec.unmarshalInputInnerDirectives(ctx, v) + res, err := ec.unmarshalInputInnerDirectives(ctx, v) + return res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) unmarshalNInnerDirectives2ᚖgithubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐInnerDirectives(ctx context.Context, v interface{}) (*InnerDirectives, error) { @@ -12510,11 +12495,12 @@ func (ec *executionContext) unmarshalNInnerDirectives2ᚖgithubᚗcomᚋ99design return nil, nil } res, err := ec.unmarshalNInnerDirectives2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐInnerDirectives(ctx, v) - return &res, err + return &res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) unmarshalNInnerInput2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐInnerInput(ctx context.Context, v interface{}) (InnerInput, error) { - return ec.unmarshalInputInnerInput(ctx, v) + res, err := ec.unmarshalInputInnerInput(ctx, v) + return res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) unmarshalNInnerInput2ᚖgithubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐInnerInput(ctx context.Context, v interface{}) (*InnerInput, error) { @@ -12522,7 +12508,7 @@ func (ec *executionContext) unmarshalNInnerInput2ᚖgithubᚗcomᚋ99designsᚋg return nil, nil } res, err := ec.unmarshalNInnerInput2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐInnerInput(ctx, v) - return &res, err + return &res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) marshalNInnerObject2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐInnerObject(ctx context.Context, sel ast.SelectionSet, v InnerObject) graphql.Marshaler { @@ -12540,11 +12526,13 @@ func (ec *executionContext) marshalNInnerObject2ᚖgithubᚗcomᚋ99designsᚋgq } func (ec *executionContext) unmarshalNInputDirectives2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐInputDirectives(ctx context.Context, v interface{}) (InputDirectives, error) { - return ec.unmarshalInputInputDirectives(ctx, v) + res, err := ec.unmarshalInputInputDirectives(ctx, v) + return res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) unmarshalNInt2int(ctx context.Context, v interface{}) (int, error) { - return graphql.UnmarshalInt(v) + res, err := graphql.UnmarshalInt(v) + return res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) marshalNInt2int(ctx context.Context, sel ast.SelectionSet, v int) graphql.Marshaler { @@ -12558,7 +12546,8 @@ func (ec *executionContext) marshalNInt2int(ctx context.Context, sel ast.Selecti } func (ec *executionContext) unmarshalNInt2int32(ctx context.Context, v interface{}) (int32, error) { - return graphql.UnmarshalInt32(v) + res, err := graphql.UnmarshalInt32(v) + return res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) marshalNInt2int32(ctx context.Context, sel ast.SelectionSet, v int32) graphql.Marshaler { @@ -12572,7 +12561,8 @@ func (ec *executionContext) marshalNInt2int32(ctx context.Context, sel ast.Selec } func (ec *executionContext) unmarshalNInt2int64(ctx context.Context, v interface{}) (int64, error) { - return graphql.UnmarshalInt64(v) + res, err := graphql.UnmarshalInt64(v) + return res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) marshalNInt2int64(ctx context.Context, sel ast.SelectionSet, v int64) graphql.Marshaler { @@ -12616,17 +12606,7 @@ func (ec *executionContext) marshalNLoopB2ᚖgithubᚗcomᚋ99designsᚋgqlgen func (ec *executionContext) unmarshalNMarshalPanic2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐMarshalPanic(ctx context.Context, v interface{}) (MarshalPanic, error) { var res MarshalPanic err := res.UnmarshalGQL(v) - if err != nil { - fic := graphql.GetFieldInputContext(ctx) - path := fic.Path() - if gerr, ok := err.(*gqlerror.Error); ok { - gerr.Path = path - return res, gerr - } else { - return res, gqlerror.WrapPath(path, err) - } - } - return res, nil + return res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) marshalNMarshalPanic2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐMarshalPanic(ctx context.Context, sel ast.SelectionSet, v MarshalPanic) graphql.Marshaler { @@ -12645,9 +12625,10 @@ func (ec *executionContext) unmarshalNMarshalPanic2ᚕgithubᚗcomᚋ99designs var err error res := make([]MarshalPanic, len(vSlice)) for i := range vSlice { - res[i], err = ec.unmarshalNMarshalPanic2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐMarshalPanic(ctx, vSlice[i]) + nctx := graphql.WithFieldInputContext(ctx, graphql.NewFieldInputWithIndex(i)) + res[i], err = ec.unmarshalNMarshalPanic2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐMarshalPanic(nctx, vSlice[i]) if err != nil { - return nil, err + return nil, graphql.WrapErrorWithInputPath(nctx, err) } } return res, nil @@ -12663,7 +12644,8 @@ func (ec *executionContext) marshalNMarshalPanic2ᚕgithubᚗcomᚋ99designsᚋg } func (ec *executionContext) unmarshalNNestedInput2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐNestedInput(ctx context.Context, v interface{}) (NestedInput, error) { - return ec.unmarshalInputNestedInput(ctx, v) + res, err := ec.unmarshalInputNestedInput(ctx, v) + return res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) unmarshalNNestedInput2ᚖgithubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐNestedInput(ctx context.Context, v interface{}) (*NestedInput, error) { @@ -12671,7 +12653,7 @@ func (ec *executionContext) unmarshalNNestedInput2ᚖgithubᚗcomᚋ99designsᚋ return nil, nil } res, err := ec.unmarshalNNestedInput2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐNestedInput(ctx, v) - return &res, err + return &res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) marshalNNode2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐNode(ctx context.Context, sel ast.SelectionSet, v Node) graphql.Marshaler { @@ -12767,7 +12749,8 @@ func (ec *executionContext) marshalNPrimitiveString2ᚕgithubᚗcomᚋ99designs } func (ec *executionContext) unmarshalNRecursiveInputSlice2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐRecursiveInputSlice(ctx context.Context, v interface{}) (RecursiveInputSlice, error) { - return ec.unmarshalInputRecursiveInputSlice(ctx, v) + res, err := ec.unmarshalInputRecursiveInputSlice(ctx, v) + return res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) marshalNShapeUnion2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐShapeUnion(ctx context.Context, sel ast.SelectionSet, v ShapeUnion) graphql.Marshaler { @@ -12781,11 +12764,13 @@ func (ec *executionContext) marshalNShapeUnion2githubᚗcomᚋ99designsᚋgqlgen } func (ec *executionContext) unmarshalNSpecialInput2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐSpecialInput(ctx context.Context, v interface{}) (SpecialInput, error) { - return ec.unmarshalInputSpecialInput(ctx, v) + res, err := ec.unmarshalInputSpecialInput(ctx, v) + return res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) unmarshalNString2string(ctx context.Context, v interface{}) (string, error) { - return graphql.UnmarshalString(v) + res, err := graphql.UnmarshalString(v) + return res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) marshalNString2string(ctx context.Context, sel ast.SelectionSet, v string) graphql.Marshaler { @@ -12810,9 +12795,10 @@ func (ec *executionContext) unmarshalNString2ᚕstringᚄ(ctx context.Context, v var err error res := make([]string, len(vSlice)) for i := range vSlice { - res[i], err = ec.unmarshalNString2string(ctx, vSlice[i]) + nctx := graphql.WithFieldInputContext(ctx, graphql.NewFieldInputWithIndex(i)) + res[i], err = ec.unmarshalNString2string(nctx, vSlice[i]) if err != nil { - return nil, err + return nil, graphql.WrapErrorWithInputPath(nctx, err) } } return res, nil @@ -12839,9 +12825,10 @@ func (ec *executionContext) unmarshalNString2ᚕᚖstring(ctx context.Context, v var err error res := make([]*string, len(vSlice)) for i := range vSlice { - res[i], err = ec.unmarshalOString2ᚖstring(ctx, vSlice[i]) + nctx := graphql.WithFieldInputContext(ctx, graphql.NewFieldInputWithIndex(i)) + res[i], err = ec.unmarshalOString2ᚖstring(nctx, vSlice[i]) if err != nil { - return nil, err + return nil, graphql.WrapErrorWithInputPath(nctx, err) } } return res, nil @@ -12861,7 +12848,7 @@ func (ec *executionContext) unmarshalNString2ᚖstring(ctx context.Context, v in return nil, nil } res, err := ec.unmarshalNString2string(ctx, v) - return &res, err + return &res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) marshalNString2ᚖstring(ctx context.Context, sel ast.SelectionSet, v *string) graphql.Marshaler { @@ -12875,7 +12862,8 @@ func (ec *executionContext) marshalNString2ᚖstring(ctx context.Context, sel as } func (ec *executionContext) unmarshalNTime2timeᚐTime(ctx context.Context, v interface{}) (time.Time, error) { - return graphql.UnmarshalTime(v) + res, err := graphql.UnmarshalTime(v) + return res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) marshalNTime2timeᚐTime(ctx context.Context, sel ast.SelectionSet, v time.Time) graphql.Marshaler { @@ -12889,7 +12877,8 @@ func (ec *executionContext) marshalNTime2timeᚐTime(ctx context.Context, sel as } func (ec *executionContext) unmarshalNUUID2string(ctx context.Context, v interface{}) (string, error) { - return graphql.UnmarshalString(v) + res, err := graphql.UnmarshalString(v) + return res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) marshalNUUID2string(ctx context.Context, sel ast.SelectionSet, v string) graphql.Marshaler { @@ -12955,7 +12944,7 @@ func (ec *executionContext) marshalNUser2ᚖgithubᚗcomᚋ99designsᚋgqlgenᚋ func (ec *executionContext) unmarshalNWrappedScalar2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐWrappedScalar(ctx context.Context, v interface{}) (WrappedScalar, error) { tmp, err := graphql.UnmarshalString(v) - return WrappedScalar(tmp), err + return WrappedScalar(tmp), graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) marshalNWrappedScalar2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐWrappedScalar(ctx context.Context, sel ast.SelectionSet, v WrappedScalar) graphql.Marshaler { @@ -13024,7 +13013,8 @@ func (ec *executionContext) marshalN__Directive2ᚕgithubᚗcomᚋ99designsᚋgq } func (ec *executionContext) unmarshalN__DirectiveLocation2string(ctx context.Context, v interface{}) (string, error) { - return graphql.UnmarshalString(v) + res, err := graphql.UnmarshalString(v) + return res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) marshalN__DirectiveLocation2string(ctx context.Context, sel ast.SelectionSet, v string) graphql.Marshaler { @@ -13049,9 +13039,10 @@ func (ec *executionContext) unmarshalN__DirectiveLocation2ᚕstringᚄ(ctx conte var err error res := make([]string, len(vSlice)) for i := range vSlice { - res[i], err = ec.unmarshalN__DirectiveLocation2string(ctx, vSlice[i]) + nctx := graphql.WithFieldInputContext(ctx, graphql.NewFieldInputWithIndex(i)) + res[i], err = ec.unmarshalN__DirectiveLocation2string(nctx, vSlice[i]) if err != nil { - return nil, err + return nil, graphql.WrapErrorWithInputPath(nctx, err) } } return res, nil @@ -13195,7 +13186,8 @@ func (ec *executionContext) marshalN__Type2ᚖgithubᚗcomᚋ99designsᚋgqlgen } func (ec *executionContext) unmarshalN__TypeKind2string(ctx context.Context, v interface{}) (string, error) { - return graphql.UnmarshalString(v) + res, err := graphql.UnmarshalString(v) + return res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) marshalN__TypeKind2string(ctx context.Context, sel ast.SelectionSet, v string) graphql.Marshaler { @@ -13234,7 +13226,8 @@ func (ec *executionContext) marshalOBackedByInterface2githubᚗcomᚋ99designs } func (ec *executionContext) unmarshalOBoolean2bool(ctx context.Context, v interface{}) (bool, error) { - return graphql.UnmarshalBoolean(v) + res, err := graphql.UnmarshalBoolean(v) + return res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) marshalOBoolean2bool(ctx context.Context, sel ast.SelectionSet, v bool) graphql.Marshaler { @@ -13246,7 +13239,7 @@ func (ec *executionContext) unmarshalOBoolean2ᚖbool(ctx context.Context, v int return nil, nil } res, err := ec.unmarshalOBoolean2bool(ctx, v) - return &res, err + return &res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) marshalOBoolean2ᚖbool(ctx context.Context, sel ast.SelectionSet, v *bool) graphql.Marshaler { @@ -13366,7 +13359,8 @@ func (ec *executionContext) marshalOCircle2ᚖgithubᚗcomᚋ99designsᚋgqlgen } func (ec *executionContext) unmarshalODefaultScalarImplementation2string(ctx context.Context, v interface{}) (string, error) { - return graphql.UnmarshalString(v) + res, err := graphql.UnmarshalString(v) + return res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) marshalODefaultScalarImplementation2string(ctx context.Context, sel ast.SelectionSet, v string) graphql.Marshaler { @@ -13378,7 +13372,7 @@ func (ec *executionContext) unmarshalODefaultScalarImplementation2ᚖstring(ctx return nil, nil } res, err := ec.unmarshalODefaultScalarImplementation2string(ctx, v) - return &res, err + return &res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) marshalODefaultScalarImplementation2ᚖstring(ctx context.Context, sel ast.SelectionSet, v *string) graphql.Marshaler { @@ -13444,7 +13438,8 @@ func (ec *executionContext) marshalOErrors2ᚖgithubᚗcomᚋ99designsᚋgqlgen } func (ec *executionContext) unmarshalOFloat2float64(ctx context.Context, v interface{}) (float64, error) { - return graphql.UnmarshalFloat(v) + res, err := graphql.UnmarshalFloat(v) + return res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) marshalOFloat2float64(ctx context.Context, sel ast.SelectionSet, v float64) graphql.Marshaler { @@ -13452,7 +13447,8 @@ func (ec *executionContext) marshalOFloat2float64(ctx context.Context, sel ast.S } func (ec *executionContext) unmarshalOInnerDirectives2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐInnerDirectives(ctx context.Context, v interface{}) (InnerDirectives, error) { - return ec.unmarshalInputInnerDirectives(ctx, v) + res, err := ec.unmarshalInputInnerDirectives(ctx, v) + return res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) unmarshalOInnerDirectives2ᚖgithubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐInnerDirectives(ctx context.Context, v interface{}) (*InnerDirectives, error) { @@ -13460,11 +13456,12 @@ func (ec *executionContext) unmarshalOInnerDirectives2ᚖgithubᚗcomᚋ99design return nil, nil } res, err := ec.unmarshalOInnerDirectives2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐInnerDirectives(ctx, v) - return &res, err + return &res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) unmarshalOInputDirectives2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐInputDirectives(ctx context.Context, v interface{}) (InputDirectives, error) { - return ec.unmarshalInputInputDirectives(ctx, v) + res, err := ec.unmarshalInputInputDirectives(ctx, v) + return res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) unmarshalOInputDirectives2ᚖgithubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐInputDirectives(ctx context.Context, v interface{}) (*InputDirectives, error) { @@ -13472,11 +13469,12 @@ func (ec *executionContext) unmarshalOInputDirectives2ᚖgithubᚗcomᚋ99design return nil, nil } res, err := ec.unmarshalOInputDirectives2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐInputDirectives(ctx, v) - return &res, err + return &res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) unmarshalOInputWithEnumValue2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐInputWithEnumValue(ctx context.Context, v interface{}) (InputWithEnumValue, error) { - return ec.unmarshalInputInputWithEnumValue(ctx, v) + res, err := ec.unmarshalInputInputWithEnumValue(ctx, v) + return res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) unmarshalOInputWithEnumValue2ᚖgithubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐInputWithEnumValue(ctx context.Context, v interface{}) (*InputWithEnumValue, error) { @@ -13484,11 +13482,12 @@ func (ec *executionContext) unmarshalOInputWithEnumValue2ᚖgithubᚗcomᚋ99des return nil, nil } res, err := ec.unmarshalOInputWithEnumValue2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐInputWithEnumValue(ctx, v) - return &res, err + return &res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) unmarshalOInt2int(ctx context.Context, v interface{}) (int, error) { - return graphql.UnmarshalInt(v) + res, err := graphql.UnmarshalInt(v) + return res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) marshalOInt2int(ctx context.Context, sel ast.SelectionSet, v int) graphql.Marshaler { @@ -13500,7 +13499,7 @@ func (ec *executionContext) unmarshalOInt2ᚖint(ctx context.Context, v interfac return nil, nil } res, err := ec.unmarshalOInt2int(ctx, v) - return &res, err + return &res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) marshalOInt2ᚖint(ctx context.Context, sel ast.SelectionSet, v *int) graphql.Marshaler { @@ -13558,7 +13557,8 @@ func (ec *executionContext) marshalOModelMethods2ᚖgithubᚗcomᚋ99designsᚋg } func (ec *executionContext) unmarshalONestedMapInput2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐNestedMapInput(ctx context.Context, v interface{}) (NestedMapInput, error) { - return ec.unmarshalInputNestedMapInput(ctx, v) + res, err := ec.unmarshalInputNestedMapInput(ctx, v) + return res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) unmarshalONestedMapInput2ᚖgithubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐNestedMapInput(ctx context.Context, v interface{}) (*NestedMapInput, error) { @@ -13566,7 +13566,7 @@ func (ec *executionContext) unmarshalONestedMapInput2ᚖgithubᚗcomᚋ99designs return nil, nil } res, err := ec.unmarshalONestedMapInput2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐNestedMapInput(ctx, v) - return &res, err + return &res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) marshalOObjectDirectives2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐObjectDirectives(ctx context.Context, sel ast.SelectionSet, v ObjectDirectives) graphql.Marshaler { @@ -13592,7 +13592,8 @@ func (ec *executionContext) marshalOObjectDirectivesWithCustomGoModel2ᚖgithub } func (ec *executionContext) unmarshalOOuterInput2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐOuterInput(ctx context.Context, v interface{}) (OuterInput, error) { - return ec.unmarshalInputOuterInput(ctx, v) + res, err := ec.unmarshalInputOuterInput(ctx, v) + return res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) unmarshalOOuterInput2ᚕᚕᚖgithubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐOuterInput(ctx context.Context, v interface{}) ([][]*OuterInput, error) { @@ -13607,9 +13608,10 @@ func (ec *executionContext) unmarshalOOuterInput2ᚕᚕᚖgithubᚗcomᚋ99desig var err error res := make([][]*OuterInput, len(vSlice)) for i := range vSlice { - res[i], err = ec.unmarshalOOuterInput2ᚕᚖgithubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐOuterInput(ctx, vSlice[i]) + nctx := graphql.WithFieldInputContext(ctx, graphql.NewFieldInputWithIndex(i)) + res[i], err = ec.unmarshalOOuterInput2ᚕᚖgithubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐOuterInput(nctx, vSlice[i]) if err != nil { - return nil, err + return nil, graphql.WrapErrorWithInputPath(nctx, err) } } return res, nil @@ -13627,9 +13629,10 @@ func (ec *executionContext) unmarshalOOuterInput2ᚕᚖgithubᚗcomᚋ99designs var err error res := make([]*OuterInput, len(vSlice)) for i := range vSlice { - res[i], err = ec.unmarshalOOuterInput2ᚖgithubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐOuterInput(ctx, vSlice[i]) + nctx := graphql.WithFieldInputContext(ctx, graphql.NewFieldInputWithIndex(i)) + res[i], err = ec.unmarshalOOuterInput2ᚖgithubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐOuterInput(nctx, vSlice[i]) if err != nil { - return nil, err + return nil, graphql.WrapErrorWithInputPath(nctx, err) } } return res, nil @@ -13640,7 +13643,7 @@ func (ec *executionContext) unmarshalOOuterInput2ᚖgithubᚗcomᚋ99designsᚋg return nil, nil } res, err := ec.unmarshalOOuterInput2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐOuterInput(ctx, v) - return &res, err + return &res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) marshalOOuterObject2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐOuterObject(ctx context.Context, sel ast.SelectionSet, v OuterObject) graphql.Marshaler { @@ -13757,7 +13760,8 @@ func (ec *executionContext) marshalOPanics2ᚖgithubᚗcomᚋ99designsᚋgqlgen } func (ec *executionContext) unmarshalORecursiveInputSlice2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐRecursiveInputSlice(ctx context.Context, v interface{}) (RecursiveInputSlice, error) { - return ec.unmarshalInputRecursiveInputSlice(ctx, v) + res, err := ec.unmarshalInputRecursiveInputSlice(ctx, v) + return res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) unmarshalORecursiveInputSlice2ᚕgithubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐRecursiveInputSliceᚄ(ctx context.Context, v interface{}) ([]RecursiveInputSlice, error) { @@ -13772,9 +13776,10 @@ func (ec *executionContext) unmarshalORecursiveInputSlice2ᚕgithubᚗcomᚋ99de var err error res := make([]RecursiveInputSlice, len(vSlice)) for i := range vSlice { - res[i], err = ec.unmarshalNRecursiveInputSlice2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐRecursiveInputSlice(ctx, vSlice[i]) + nctx := graphql.WithFieldInputContext(ctx, graphql.NewFieldInputWithIndex(i)) + res[i], err = ec.unmarshalNRecursiveInputSlice2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐRecursiveInputSlice(nctx, vSlice[i]) if err != nil { - return nil, err + return nil, graphql.WrapErrorWithInputPath(nctx, err) } } return res, nil @@ -13785,7 +13790,7 @@ func (ec *executionContext) unmarshalORecursiveInputSlice2ᚖgithubᚗcomᚋ99de return nil, nil } res, err := ec.unmarshalORecursiveInputSlice2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐRecursiveInputSlice(ctx, v) - return &res, err + return &res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) marshalOShape2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐShape(ctx context.Context, sel ast.SelectionSet, v Shape) graphql.Marshaler { @@ -13847,7 +13852,8 @@ func (ec *executionContext) marshalOSlices2ᚖgithubᚗcomᚋ99designsᚋgqlgen } func (ec *executionContext) unmarshalOString2string(ctx context.Context, v interface{}) (string, error) { - return graphql.UnmarshalString(v) + res, err := graphql.UnmarshalString(v) + return res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) marshalOString2string(ctx context.Context, sel ast.SelectionSet, v string) graphql.Marshaler { @@ -13866,9 +13872,10 @@ func (ec *executionContext) unmarshalOString2ᚕstringᚄ(ctx context.Context, v var err error res := make([]string, len(vSlice)) for i := range vSlice { - res[i], err = ec.unmarshalNString2string(ctx, vSlice[i]) + nctx := graphql.WithFieldInputContext(ctx, graphql.NewFieldInputWithIndex(i)) + res[i], err = ec.unmarshalNString2string(nctx, vSlice[i]) if err != nil { - return nil, err + return nil, graphql.WrapErrorWithInputPath(nctx, err) } } return res, nil @@ -13898,9 +13905,10 @@ func (ec *executionContext) unmarshalOString2ᚕᚖstring(ctx context.Context, v var err error res := make([]*string, len(vSlice)) for i := range vSlice { - res[i], err = ec.unmarshalOString2ᚖstring(ctx, vSlice[i]) + nctx := graphql.WithFieldInputContext(ctx, graphql.NewFieldInputWithIndex(i)) + res[i], err = ec.unmarshalOString2ᚖstring(nctx, vSlice[i]) if err != nil { - return nil, err + return nil, graphql.WrapErrorWithInputPath(nctx, err) } } return res, nil @@ -13923,7 +13931,7 @@ func (ec *executionContext) unmarshalOString2ᚖstring(ctx context.Context, v in return nil, nil } res, err := ec.unmarshalOString2string(ctx, v) - return &res, err + return &res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) marshalOString2ᚖstring(ctx context.Context, sel ast.SelectionSet, v *string) graphql.Marshaler { @@ -13941,7 +13949,8 @@ func (ec *executionContext) marshalOTestUnion2githubᚗcomᚋ99designsᚋgqlgen } func (ec *executionContext) unmarshalOThirdParty2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐThirdParty(ctx context.Context, v interface{}) (ThirdParty, error) { - return UnmarshalThirdParty(v) + res, err := UnmarshalThirdParty(v) + return res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) marshalOThirdParty2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐThirdParty(ctx context.Context, sel ast.SelectionSet, v ThirdParty) graphql.Marshaler { @@ -13953,7 +13962,7 @@ func (ec *executionContext) unmarshalOThirdParty2ᚖgithubᚗcomᚋ99designsᚋg return nil, nil } res, err := ec.unmarshalOThirdParty2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐThirdParty(ctx, v) - return &res, err + return &res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) marshalOThirdParty2ᚖgithubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐThirdParty(ctx context.Context, sel ast.SelectionSet, v *ThirdParty) graphql.Marshaler { @@ -13964,7 +13973,8 @@ func (ec *executionContext) marshalOThirdParty2ᚖgithubᚗcomᚋ99designsᚋgql } func (ec *executionContext) unmarshalOTime2timeᚐTime(ctx context.Context, v interface{}) (time.Time, error) { - return graphql.UnmarshalTime(v) + res, err := graphql.UnmarshalTime(v) + return res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) marshalOTime2timeᚐTime(ctx context.Context, sel ast.SelectionSet, v time.Time) graphql.Marshaler { @@ -13976,7 +13986,7 @@ func (ec *executionContext) unmarshalOTime2ᚖtimeᚐTime(ctx context.Context, v return nil, nil } res, err := ec.unmarshalOTime2timeᚐTime(ctx, v) - return &res, err + return &res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) marshalOTime2ᚖtimeᚐTime(ctx context.Context, sel ast.SelectionSet, v *time.Time) graphql.Marshaler { @@ -13987,7 +13997,8 @@ func (ec *executionContext) marshalOTime2ᚖtimeᚐTime(ctx context.Context, sel } func (ec *executionContext) unmarshalOValidInput2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐValidInput(ctx context.Context, v interface{}) (ValidInput, error) { - return ec.unmarshalInputValidInput(ctx, v) + res, err := ec.unmarshalInputValidInput(ctx, v) + return res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) unmarshalOValidInput2ᚖgithubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐValidInput(ctx context.Context, v interface{}) (*ValidInput, error) { @@ -13995,7 +14006,7 @@ func (ec *executionContext) unmarshalOValidInput2ᚖgithubᚗcomᚋ99designsᚋg return nil, nil } res, err := ec.unmarshalOValidInput2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐValidInput(ctx, v) - return &res, err + return &res, graphql.WrapErrorWithInputPath(ctx, err) } func (ec *executionContext) marshalOValidType2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐValidType(ctx context.Context, sel ast.SelectionSet, v ValidType) graphql.Marshaler { diff --git a/codegen/testserver/mutation_with_custom_scalar_test.go b/codegen/testserver/mutation_with_custom_scalar_test.go index fab341b1a36..fbad2189590 100644 --- a/codegen/testserver/mutation_with_custom_scalar_test.go +++ b/codegen/testserver/mutation_with_custom_scalar_test.go @@ -2,7 +2,6 @@ package testserver import ( "context" - "encoding/json" "github.com/99designs/gqlgen/client" "github.com/99designs/gqlgen/graphql/handler" "github.com/stretchr/testify/require" @@ -45,19 +44,6 @@ func TestErrorInsideMutationArgument(t *testing.T) { &resp, client.Var("input", input), ) - jsonErr, ok := err.(client.RawJsonError) - require.True(t, ok) - var errDetails []map[string]interface{} - err = json.Unmarshal(jsonErr.RawMessage, &errDetails) - require.NoError(t, err) - require.Len(t, errDetails, 1) - firstErr := errDetails[0] - path, ok := firstErr["path"].([]interface{}) - require.Equal(t, path, []interface{}{ - "updateSomething", - "input", - "nesting", - "field", - }) + require.EqualError(t, err, `[{"message":"invalid email format","path":["updateSomething","input","nesting","field"]}]`) }) } diff --git a/codegen/type.gotpl b/codegen/type.gotpl index 06a285e3f57..b30154ee243 100644 --- a/codegen/type.gotpl +++ b/codegen/type.gotpl @@ -6,7 +6,7 @@ {{- end }} {{- if $type.IsPtr }} res, err := ec.{{ $type.Elem.UnmarshalFunc }}(ctx, v) - return &res, err + return &res, graphql.WrapErrorWithInputPath(ctx, err) {{- else if $type.IsSlice }} var vSlice []interface{} if v != nil { @@ -19,9 +19,10 @@ var err error res := make([]{{$type.GO.Elem | ref}}, len(vSlice)) for i := range vSlice { - res[i], err = ec.{{ $type.Elem.UnmarshalFunc }}(ctx, vSlice[i]) + nctx := graphql.WithFieldInputContext(ctx, graphql.NewFieldInputWithIndex(i)) + res[i], err = ec.{{ $type.Elem.UnmarshalFunc }}(nctx, vSlice[i]) if err != nil { - return nil, err + return nil, graphql.WrapErrorWithInputPath(nctx, err) } } return res, nil @@ -29,28 +30,20 @@ {{- if $type.Unmarshaler }} {{- if $type.CastType }} tmp, err := {{ $type.Unmarshaler | call }}(v) - return {{ $type.GO | ref }}(tmp), err + return {{ $type.GO | ref }}(tmp), graphql.WrapErrorWithInputPath(ctx, err) {{- else}} - return {{ $type.Unmarshaler | call }}(v) + res, err := {{ $type.Unmarshaler | call }}(v) + return res, graphql.WrapErrorWithInputPath(ctx, err) {{- end }} {{- else if eq ($type.GO | ref) "map[string]interface{}" }} return v.(map[string]interface{}), nil {{- else if $type.IsMarshaler }} var res {{ $type.GO | ref }} err := res.UnmarshalGQL(v) - if err != nil { - fic := graphql.GetFieldInputContext(ctx) - path := fic.Path() - if gerr, ok := err.(*gqlerror.Error); ok { - gerr.Path = path - return res, gerr - } else { - return res, gqlerror.WrapPath(path, err) - } - } - return res, nil + return res, graphql.WrapErrorWithInputPath(ctx, err) {{- else }} - return ec.unmarshalInput{{ $type.GQL.Name }}(ctx, v) + res, err := ec.unmarshalInput{{ $type.GQL.Name }}(ctx, v) + return res, graphql.WrapErrorWithInputPath(ctx, err) {{- end }} {{- end }} } diff --git a/docs/content/reference/scalars.md b/docs/content/reference/scalars.md index d3858c80aa1..d8d9ae4be18 100644 --- a/docs/content/reference/scalars.md +++ b/docs/content/reference/scalars.md @@ -148,3 +148,57 @@ models: ``` See the [example/scalars](https://github.com/99designs/gqlgen/tree/master/example/scalars) package for more examples. + +## Unmarshaling Errors + +The errors that occur as part of custom scalar unmarshaling will return a full path to the field. +For example, given the following schema ... + +```graphql +extend type Mutation{ + updateUser(userInput: UserInput!): User! +} + +input UserInput { + name: String! + primaryContactDetails: ContactDetailsInput! + secondaryContactDetails: ContactDetailsInput! +} + +scalar Email +input ContactDetailsInput { + email: Email! +} +``` + +... and the following variables: + +```json + +{ + "userInput": { + "name": "George", + "primaryContactDetails": { + "email": "not-an-email" + }, + "secondaryContactDetails": { + "email": "george@gmail.com" + } + } +} +``` + +... and an unmarshal function that returns an error if the email is invalid. The mutation will return an error containing the full path: +```json +{ + "message": "email invalid", + "path": [ + "updateUser", + "userInput", + "primaryContactDetails", + "email" + ] +} +``` + + diff --git a/graphql/context_field_input.go b/graphql/context_field_input.go index 37a0319b7b2..84fe9882854 100644 --- a/graphql/context_field_input.go +++ b/graphql/context_field_input.go @@ -3,6 +3,7 @@ package graphql import ( "context" "github.com/vektah/gqlparser/v2/ast" + "github.com/vektah/gqlparser/v2/gqlerror" ) const fieldInputCtx key = "field_input_context" @@ -64,3 +65,20 @@ func GetFieldInputContext(ctx context.Context) *FieldInputContext { } return nil } + +func WrapErrorWithInputPath(ctx context.Context, err error) error { + if err == nil { + return nil + } + + inputContext := GetFieldInputContext(ctx) + path := inputContext.Path() + if gerr, ok := err.(*gqlerror.Error); ok { + if gerr.Path == nil { + gerr.Path = path + } + return gerr + } else { + return gqlerror.WrapPath(path, err) + } +}