From 34d109754e83d85262d53a5b7df098e07908007c Mon Sep 17 00:00:00 2001 From: Adam Date: Wed, 7 Aug 2019 17:36:50 +1000 Subject: [PATCH] Fix directives returning nils from optional interfaces --- codegen/config/binder.go | 3 + codegen/interface.gotpl | 4 +- codegen/testserver/generated.go | 229 ++++++++++++++------- codegen/testserver/interfaces.graphql | 20 ++ codegen/testserver/interfaces_test.go | 27 +++ codegen/testserver/resolver.go | 9 +- codegen/testserver/schema.graphql | 15 -- codegen/testserver/stub.go | 12 +- example/selection/generated.go | 12 +- example/starwars/generated/exec.go | 29 ++- example/type-system-extension/generated.go | 8 +- 11 files changed, 261 insertions(+), 107 deletions(-) create mode 100644 codegen/testserver/interfaces.graphql diff --git a/codegen/config/binder.go b/codegen/config/binder.go index bc7285d69ab..ed677bcb2a7 100644 --- a/codegen/config/binder.go +++ b/codegen/config/binder.go @@ -427,6 +427,9 @@ func (b *Binder) CopyModifiersFromAst(t *ast.Type, base types.Type) types.Type { } func isNilable(t types.Type) bool { + if namedType, isNamed := t.(*types.Named); isNamed { + t = namedType.Underlying() + } _, isPtr := t.(*types.Pointer) _, isMap := t.(*types.Map) _, isInterface := t.(*types.Interface) diff --git a/codegen/interface.gotpl b/codegen/interface.gotpl index 81a5807653c..47145500e48 100644 --- a/codegen/interface.gotpl +++ b/codegen/interface.gotpl @@ -1,7 +1,7 @@ {{- range $interface := .Interfaces }} -func (ec *executionContext) _{{$interface.Name}}(ctx context.Context, sel ast.SelectionSet, obj *{{$interface.Type | ref}}) graphql.Marshaler { - switch obj := (*obj).(type) { +func (ec *executionContext) _{{$interface.Name}}(ctx context.Context, sel ast.SelectionSet, obj {{$interface.Type | ref}}) graphql.Marshaler { + switch obj := (obj).(type) { case nil: return graphql.Null {{- range $implementor := $interface.Implementors }} diff --git a/codegen/testserver/generated.go b/codegen/testserver/generated.go index 02720adbd9d..e9a135504d2 100644 --- a/codegen/testserver/generated.go +++ b/codegen/testserver/generated.go @@ -62,6 +62,8 @@ type DirectiveRoot struct { Logged func(ctx context.Context, obj interface{}, next graphql.Resolver, id string) (res interface{}, err error) + MakeNil func(ctx context.Context, obj interface{}, next graphql.Resolver) (res interface{}, err error) + Range func(ctx context.Context, obj interface{}, next graphql.Resolver, min *int, max *int) (res interface{}, err error) ToNull func(ctx context.Context, obj interface{}, next graphql.Resolver) (res interface{}, err error) @@ -233,6 +235,7 @@ type ComplexityRoot struct { ModelMethods func(childComplexity int) int NestedInputs func(childComplexity int, input [][]*OuterInput) int NestedOutputs func(childComplexity int) int + NoShape func(childComplexity int) int NullableArg func(childComplexity int, arg *int) int OptionalUnion func(childComplexity int) int Overlapping func(childComplexity int) int @@ -344,7 +347,6 @@ type QueryResolver interface { Recursive(ctx context.Context, input *RecursiveInputSlice) (*bool, error) NestedInputs(ctx context.Context, input [][]*OuterInput) (*bool, error) NestedOutputs(ctx context.Context) ([][]*OuterObject, error) - Shapes(ctx context.Context) ([]Shape, error) ModelMethods(ctx context.Context) (*ModelMethods, error) User(ctx context.Context, id int) (*User, error) NullableArg(ctx context.Context, arg *int) (*string, error) @@ -364,6 +366,8 @@ type QueryResolver interface { DirectiveField(ctx context.Context) (*string, error) DirectiveDouble(ctx context.Context) (*string, error) DirectiveUnimplemented(ctx context.Context) (*string, error) + Shapes(ctx context.Context) ([]Shape, error) + NoShape(ctx context.Context) (Shape, error) MapStringInterface(ctx context.Context, in map[string]interface{}) (map[string]interface{}, error) MapNestedStringInterface(ctx context.Context, in *NestedMapInput) (map[string]interface{}, error) ErrorBubble(ctx context.Context) (*Error, error) @@ -1029,6 +1033,13 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return e.complexity.Query.NestedOutputs(childComplexity), true + case "Query.noShape": + if e.complexity.Query.NoShape == nil { + break + } + + return e.complexity.Query.NoShape(childComplexity), true + case "Query.nullableArg": if e.complexity.Query.NullableArg == nil { break @@ -1507,6 +1518,27 @@ type ObjectDirectives { type ObjectDirectivesWithCustomGoModel { nullableText: String @toNull } +`}, + &ast.Source{Name: "interfaces.graphql", Input: `extend type Query { + shapes: [Shape] + noShape: Shape @makeNil +} + +interface Shape { + area: Float +} +type Circle implements Shape { + radius: Float + area: Float +} +type Rectangle implements Shape { + length: Float + width: Float + area: Float +} +union ShapeUnion = Circle | Rectangle + +directive @makeNil on FIELD_DEFINITION `}, &ast.Source{Name: "loops.graphql", Input: `type LoopA { b: LoopB! @@ -1603,7 +1635,6 @@ type EmbeddedDefaultScalar { recursive(input: RecursiveInputSlice): Boolean nestedInputs(input: [[OuterInput]] = [[{inner: {id: 1}}]]): Boolean nestedOutputs: [[OuterObject]] - shapes: [Shape] modelMethods: ModelMethods user(id: Int!): User! nullableArg(arg: Int = 123): String @@ -1675,20 +1706,6 @@ type InnerObject { id: Int! } -interface Shape { - area: Float -} -type Circle implements Shape { - radius: Float - area: Float -} -type Rectangle implements Shape { - length: Float - width: Float - area: Float -} -union ShapeUnion = Circle | Rectangle - type ForcedResolver { field: Circle } @@ -4812,37 +4829,6 @@ func (ec *executionContext) _Query_nestedOutputs(ctx context.Context, field grap return ec.marshalOOuterObject2ᚕᚕᚖgithubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐOuterObject(ctx, field.Selections, res) } -func (ec *executionContext) _Query_shapes(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { - ctx = ec.Tracer.StartFieldExecution(ctx, field) - defer func() { - if r := recover(); r != nil { - ec.Error(ctx, ec.Recover(ctx, r)) - ret = graphql.Null - } - ec.Tracer.EndFieldExecution(ctx) - }() - rctx := &graphql.ResolverContext{ - Object: "Query", - Field: field, - Args: nil, - IsMethod: true, - } - ctx = graphql.WithResolverContext(ctx, rctx) - ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) - resTmp := ec._fieldMiddleware(ctx, nil, func(rctx context.Context) (interface{}, error) { - ctx = rctx // use context from middleware stack in children - return ec.resolvers.Query().Shapes(rctx) - }) - - if resTmp == nil { - return graphql.Null - } - res := resTmp.([]Shape) - rctx.Result = res - ctx = ec.Tracer.StartFieldChildExecution(ctx) - return ec.marshalOShape2ᚕgithubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐShape(ctx, field.Selections, res) -} - func (ec *executionContext) _Query_modelMethods(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { ctx = ec.Tracer.StartFieldExecution(ctx, field) defer func() { @@ -5584,6 +5570,88 @@ func (ec *executionContext) _Query_directiveUnimplemented(ctx context.Context, f return ec.marshalOString2ᚖstring(ctx, field.Selections, res) } +func (ec *executionContext) _Query_shapes(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { + ctx = ec.Tracer.StartFieldExecution(ctx, field) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + ec.Tracer.EndFieldExecution(ctx) + }() + rctx := &graphql.ResolverContext{ + Object: "Query", + Field: field, + Args: nil, + IsMethod: true, + } + ctx = graphql.WithResolverContext(ctx, rctx) + ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) + resTmp := ec._fieldMiddleware(ctx, nil, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return ec.resolvers.Query().Shapes(rctx) + }) + + if resTmp == nil { + return graphql.Null + } + res := resTmp.([]Shape) + rctx.Result = res + ctx = ec.Tracer.StartFieldChildExecution(ctx) + return ec.marshalOShape2ᚕgithubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐShape(ctx, field.Selections, res) +} + +func (ec *executionContext) _Query_noShape(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { + ctx = ec.Tracer.StartFieldExecution(ctx, field) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + ec.Tracer.EndFieldExecution(ctx) + }() + rctx := &graphql.ResolverContext{ + Object: "Query", + Field: field, + Args: nil, + IsMethod: true, + } + ctx = graphql.WithResolverContext(ctx, rctx) + ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) + resTmp := ec._fieldMiddleware(ctx, nil, func(rctx context.Context) (interface{}, error) { + directive0 := func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return ec.resolvers.Query().NoShape(rctx) + } + directive1 := func(ctx context.Context) (interface{}, error) { + if ec.directives.MakeNil == nil { + return nil, errors.New("directive makeNil is not implemented") + } + return ec.directives.MakeNil(ctx, nil, directive0) + } + + tmp, err := directive1(rctx) + if err != nil { + return nil, err + } + if tmp == nil { + return nil, nil + } + if data, ok := tmp.(Shape); ok { + return data, nil + } + return nil, fmt.Errorf(`unexpected type %T from directive, should be github.com/99designs/gqlgen/codegen/testserver.Shape`, tmp) + }) + + if resTmp == nil { + return graphql.Null + } + res := resTmp.(Shape) + rctx.Result = res + ctx = ec.Tracer.StartFieldChildExecution(ctx) + return ec.marshalOShape2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐShape(ctx, field.Selections, res) +} + func (ec *executionContext) _Query_mapStringInterface(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { ctx = ec.Tracer.StartFieldExecution(ctx, field) defer func() { @@ -8628,8 +8696,8 @@ func (ec *executionContext) unmarshalInputValidInput(ctx context.Context, obj in // region ************************** interface.gotpl *************************** -func (ec *executionContext) _Content_Child(ctx context.Context, sel ast.SelectionSet, obj *ContentChild) graphql.Marshaler { - switch obj := (*obj).(type) { +func (ec *executionContext) _Content_Child(ctx context.Context, sel ast.SelectionSet, obj ContentChild) graphql.Marshaler { + switch obj := (obj).(type) { case nil: return graphql.Null case ContentUser: @@ -8645,8 +8713,8 @@ func (ec *executionContext) _Content_Child(ctx context.Context, sel ast.Selectio } } -func (ec *executionContext) _Shape(ctx context.Context, sel ast.SelectionSet, obj *Shape) graphql.Marshaler { - switch obj := (*obj).(type) { +func (ec *executionContext) _Shape(ctx context.Context, sel ast.SelectionSet, obj Shape) graphql.Marshaler { + switch obj := (obj).(type) { case nil: return graphql.Null case *Circle: @@ -8658,8 +8726,8 @@ func (ec *executionContext) _Shape(ctx context.Context, sel ast.SelectionSet, ob } } -func (ec *executionContext) _ShapeUnion(ctx context.Context, sel ast.SelectionSet, obj *ShapeUnion) graphql.Marshaler { - switch obj := (*obj).(type) { +func (ec *executionContext) _ShapeUnion(ctx context.Context, sel ast.SelectionSet, obj ShapeUnion) graphql.Marshaler { + switch obj := (obj).(type) { case nil: return graphql.Null case *Circle: @@ -8671,8 +8739,8 @@ func (ec *executionContext) _ShapeUnion(ctx context.Context, sel ast.SelectionSe } } -func (ec *executionContext) _TestUnion(ctx context.Context, sel ast.SelectionSet, obj *TestUnion) graphql.Marshaler { - switch obj := (*obj).(type) { +func (ec *executionContext) _TestUnion(ctx context.Context, sel ast.SelectionSet, obj TestUnion) graphql.Marshaler { + switch obj := (obj).(type) { case nil: return graphql.Null case A: @@ -9755,17 +9823,6 @@ func (ec *executionContext) _Query(ctx context.Context, sel ast.SelectionSet) gr res = ec._Query_nestedOutputs(ctx, field) return res }) - case "shapes": - field := field - out.Concurrently(i, func() (res graphql.Marshaler) { - defer func() { - if r := recover(); r != nil { - ec.Error(ctx, ec.Recover(ctx, r)) - } - }() - res = ec._Query_shapes(ctx, field) - return res - }) case "modelMethods": field := field out.Concurrently(i, func() (res graphql.Marshaler) { @@ -9990,6 +10047,28 @@ func (ec *executionContext) _Query(ctx context.Context, sel ast.SelectionSet) gr res = ec._Query_directiveUnimplemented(ctx, field) return res }) + case "shapes": + field := field + out.Concurrently(i, func() (res graphql.Marshaler) { + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + } + }() + res = ec._Query_shapes(ctx, field) + return res + }) + case "noShape": + field := field + out.Concurrently(i, func() (res graphql.Marshaler) { + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + } + }() + res = ec._Query_noShape(ctx, field) + return res + }) case "mapStringInterface": field := field out.Concurrently(i, func() (res graphql.Marshaler) { @@ -11105,7 +11184,13 @@ func (ec *executionContext) unmarshalNRecursiveInputSlice2githubᚗcomᚋ99desig } func (ec *executionContext) marshalNShapeUnion2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐShapeUnion(ctx context.Context, sel ast.SelectionSet, v ShapeUnion) graphql.Marshaler { - return ec._ShapeUnion(ctx, sel, &v) + if v == nil { + if !ec.HasError(graphql.GetResolverContext(ctx)) { + ec.Errorf(ctx, "must not be null") + } + return graphql.Null + } + return ec._ShapeUnion(ctx, sel, v) } func (ec *executionContext) unmarshalNString2string(ctx context.Context, v interface{}) (string, error) { @@ -11963,7 +12048,10 @@ func (ec *executionContext) unmarshalORecursiveInputSlice2ᚖgithubᚗcomᚋ99de } func (ec *executionContext) marshalOShape2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐShape(ctx context.Context, sel ast.SelectionSet, v Shape) graphql.Marshaler { - return ec._Shape(ctx, sel, &v) + if v == nil { + return graphql.Null + } + return ec._Shape(ctx, sel, v) } func (ec *executionContext) marshalOShape2ᚕgithubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐShape(ctx context.Context, sel ast.SelectionSet, v []Shape) graphql.Marshaler { @@ -12105,7 +12193,10 @@ func (ec *executionContext) marshalOString2ᚖstring(ctx context.Context, sel as } func (ec *executionContext) marshalOTestUnion2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐTestUnion(ctx context.Context, sel ast.SelectionSet, v TestUnion) graphql.Marshaler { - return ec._TestUnion(ctx, sel, &v) + if v == nil { + return graphql.Null + } + return ec._TestUnion(ctx, sel, v) } func (ec *executionContext) unmarshalOThirdParty2githubᚗcomᚋ99designsᚋgqlgenᚋcodegenᚋtestserverᚐThirdParty(ctx context.Context, v interface{}) (ThirdParty, error) { diff --git a/codegen/testserver/interfaces.graphql b/codegen/testserver/interfaces.graphql new file mode 100644 index 00000000000..44fb6538daf --- /dev/null +++ b/codegen/testserver/interfaces.graphql @@ -0,0 +1,20 @@ +extend type Query { + shapes: [Shape] + noShape: Shape @makeNil +} + +interface Shape { + area: Float +} +type Circle implements Shape { + radius: Float + area: Float +} +type Rectangle implements Shape { + length: Float + width: Float + area: Float +} +union ShapeUnion = Circle | Rectangle + +directive @makeNil on FIELD_DEFINITION diff --git a/codegen/testserver/interfaces_test.go b/codegen/testserver/interfaces_test.go index d10f48b071c..88c3fe61ee9 100644 --- a/codegen/testserver/interfaces_test.go +++ b/codegen/testserver/interfaces_test.go @@ -1,9 +1,13 @@ package testserver import ( + "context" "reflect" "testing" + "github.com/99designs/gqlgen/client" + "github.com/99designs/gqlgen/graphql" + "github.com/99designs/gqlgen/handler" "github.com/stretchr/testify/require" ) @@ -13,4 +17,27 @@ func TestInterfaces(t *testing.T) { require.True(t, ok) require.Equal(t, "[]testserver.Shape", field.Type.Out(0).String()) }) + + t.Run("interfaces can be nil", func(t *testing.T) { + resolvers := &Stub{} + resolvers.QueryResolver.NoShape = func(ctx context.Context) (shapes Shape, e error) { + return nil, nil + } + + srv := handler.GraphQL( + NewExecutableSchema(Config{ + Resolvers: resolvers, + Directives: DirectiveRoot{ + MakeNil: func(ctx context.Context, obj interface{}, next graphql.Resolver) (res interface{}, err error) { + return nil, nil + }, + }, + }), + ) + + c := client.New(srv) + + var resp interface{} + c.MustPost(`{ noShape { area } }`, &resp) + }) } diff --git a/codegen/testserver/resolver.go b/codegen/testserver/resolver.go index 997203cc058..29df1b49451 100644 --- a/codegen/testserver/resolver.go +++ b/codegen/testserver/resolver.go @@ -122,9 +122,6 @@ func (r *queryResolver) NestedInputs(ctx context.Context, input [][]*OuterInput) func (r *queryResolver) NestedOutputs(ctx context.Context) ([][]*OuterObject, error) { panic("not implemented") } -func (r *queryResolver) Shapes(ctx context.Context) ([]Shape, error) { - panic("not implemented") -} func (r *queryResolver) ModelMethods(ctx context.Context) (*ModelMethods, error) { panic("not implemented") } @@ -182,6 +179,12 @@ func (r *queryResolver) DirectiveDouble(ctx context.Context) (*string, error) { func (r *queryResolver) DirectiveUnimplemented(ctx context.Context) (*string, error) { panic("not implemented") } +func (r *queryResolver) Shapes(ctx context.Context) ([]Shape, error) { + panic("not implemented") +} +func (r *queryResolver) NoShape(ctx context.Context) (Shape, error) { + panic("not implemented") +} func (r *queryResolver) MapStringInterface(ctx context.Context, in map[string]interface{}) (map[string]interface{}, error) { panic("not implemented") } diff --git a/codegen/testserver/schema.graphql b/codegen/testserver/schema.graphql index 32fd3288914..7e3393a2101 100644 --- a/codegen/testserver/schema.graphql +++ b/codegen/testserver/schema.graphql @@ -5,7 +5,6 @@ type Query { recursive(input: RecursiveInputSlice): Boolean nestedInputs(input: [[OuterInput]] = [[{inner: {id: 1}}]]): Boolean nestedOutputs: [[OuterObject]] - shapes: [Shape] modelMethods: ModelMethods user(id: Int!): User! nullableArg(arg: Int = 123): String @@ -77,20 +76,6 @@ type InnerObject { id: Int! } -interface Shape { - area: Float -} -type Circle implements Shape { - radius: Float - area: Float -} -type Rectangle implements Shape { - length: Float - width: Float - area: Float -} -union ShapeUnion = Circle | Rectangle - type ForcedResolver { field: Circle } diff --git a/codegen/testserver/stub.go b/codegen/testserver/stub.go index 7f1c40dc64f..bcc7ad7f243 100644 --- a/codegen/testserver/stub.go +++ b/codegen/testserver/stub.go @@ -44,7 +44,6 @@ type Stub struct { Recursive func(ctx context.Context, input *RecursiveInputSlice) (*bool, error) NestedInputs func(ctx context.Context, input [][]*OuterInput) (*bool, error) NestedOutputs func(ctx context.Context) ([][]*OuterObject, error) - Shapes func(ctx context.Context) ([]Shape, error) ModelMethods func(ctx context.Context) (*ModelMethods, error) User func(ctx context.Context, id int) (*User, error) NullableArg func(ctx context.Context, arg *int) (*string, error) @@ -64,6 +63,8 @@ type Stub struct { DirectiveField func(ctx context.Context) (*string, error) DirectiveDouble func(ctx context.Context) (*string, error) DirectiveUnimplemented func(ctx context.Context) (*string, error) + Shapes func(ctx context.Context) ([]Shape, error) + NoShape func(ctx context.Context) (Shape, error) MapStringInterface func(ctx context.Context, in map[string]interface{}) (map[string]interface{}, error) MapNestedStringInterface func(ctx context.Context, in *NestedMapInput) (map[string]interface{}, error) ErrorBubble func(ctx context.Context) (*Error, error) @@ -205,9 +206,6 @@ func (r *stubQuery) NestedInputs(ctx context.Context, input [][]*OuterInput) (*b func (r *stubQuery) NestedOutputs(ctx context.Context) ([][]*OuterObject, error) { return r.QueryResolver.NestedOutputs(ctx) } -func (r *stubQuery) Shapes(ctx context.Context) ([]Shape, error) { - return r.QueryResolver.Shapes(ctx) -} func (r *stubQuery) ModelMethods(ctx context.Context) (*ModelMethods, error) { return r.QueryResolver.ModelMethods(ctx) } @@ -265,6 +263,12 @@ func (r *stubQuery) DirectiveDouble(ctx context.Context) (*string, error) { func (r *stubQuery) DirectiveUnimplemented(ctx context.Context) (*string, error) { return r.QueryResolver.DirectiveUnimplemented(ctx) } +func (r *stubQuery) Shapes(ctx context.Context) ([]Shape, error) { + return r.QueryResolver.Shapes(ctx) +} +func (r *stubQuery) NoShape(ctx context.Context) (Shape, error) { + return r.QueryResolver.NoShape(ctx) +} func (r *stubQuery) MapStringInterface(ctx context.Context, in map[string]interface{}) (map[string]interface{}, error) { return r.QueryResolver.MapStringInterface(ctx, in) } diff --git a/example/selection/generated.go b/example/selection/generated.go index bfd540e351f..206fef77566 100644 --- a/example/selection/generated.go +++ b/example/selection/generated.go @@ -1821,8 +1821,8 @@ func (ec *executionContext) ___Type_ofType(ctx context.Context, field graphql.Co // region ************************** interface.gotpl *************************** -func (ec *executionContext) _Event(ctx context.Context, sel ast.SelectionSet, obj *Event) graphql.Marshaler { - switch obj := (*obj).(type) { +func (ec *executionContext) _Event(ctx context.Context, sel ast.SelectionSet, obj Event) graphql.Marshaler { + switch obj := (obj).(type) { case nil: return graphql.Null case Post: @@ -2215,7 +2215,13 @@ func (ec *executionContext) marshalNBoolean2bool(ctx context.Context, sel ast.Se } func (ec *executionContext) marshalNEvent2githubᚗcomᚋ99designsᚋgqlgenᚋexampleᚋselectionᚐEvent(ctx context.Context, sel ast.SelectionSet, v Event) graphql.Marshaler { - return ec._Event(ctx, sel, &v) + if v == nil { + if !ec.HasError(graphql.GetResolverContext(ctx)) { + ec.Errorf(ctx, "must not be null") + } + return graphql.Null + } + return ec._Event(ctx, sel, v) } func (ec *executionContext) unmarshalNString2string(ctx context.Context, v interface{}) (string, error) { diff --git a/example/starwars/generated/exec.go b/example/starwars/generated/exec.go index 2c6c8704973..bca57b48a19 100644 --- a/example/starwars/generated/exec.go +++ b/example/starwars/generated/exec.go @@ -3638,8 +3638,8 @@ func (ec *executionContext) unmarshalInputReviewInput(ctx context.Context, obj i // region ************************** interface.gotpl *************************** -func (ec *executionContext) _Character(ctx context.Context, sel ast.SelectionSet, obj *models.Character) graphql.Marshaler { - switch obj := (*obj).(type) { +func (ec *executionContext) _Character(ctx context.Context, sel ast.SelectionSet, obj models.Character) graphql.Marshaler { + switch obj := (obj).(type) { case nil: return graphql.Null case models.Human: @@ -3655,8 +3655,8 @@ func (ec *executionContext) _Character(ctx context.Context, sel ast.SelectionSet } } -func (ec *executionContext) _SearchResult(ctx context.Context, sel ast.SelectionSet, obj *models.SearchResult) graphql.Marshaler { - switch obj := (*obj).(type) { +func (ec *executionContext) _SearchResult(ctx context.Context, sel ast.SelectionSet, obj models.SearchResult) graphql.Marshaler { + switch obj := (obj).(type) { case nil: return graphql.Null case models.Human: @@ -4427,7 +4427,13 @@ func (ec *executionContext) marshalNBoolean2bool(ctx context.Context, sel ast.Se } func (ec *executionContext) marshalNCharacter2githubᚗcomᚋ99designsᚋgqlgenᚋexampleᚋstarwarsᚋmodelsᚐCharacter(ctx context.Context, sel ast.SelectionSet, v models.Character) graphql.Marshaler { - return ec._Character(ctx, sel, &v) + if v == nil { + if !ec.HasError(graphql.GetResolverContext(ctx)) { + ec.Errorf(ctx, "must not be null") + } + return graphql.Null + } + return ec._Character(ctx, sel, v) } func (ec *executionContext) unmarshalNEpisode2githubᚗcomᚋ99designsᚋgqlgenᚋexampleᚋstarwarsᚋmodelsᚐEpisode(ctx context.Context, v interface{}) (models.Episode, error) { @@ -4684,7 +4690,13 @@ func (ec *executionContext) unmarshalNReviewInput2githubᚗcomᚋ99designsᚋgql } func (ec *executionContext) marshalNSearchResult2githubᚗcomᚋ99designsᚋgqlgenᚋexampleᚋstarwarsᚋmodelsᚐSearchResult(ctx context.Context, sel ast.SelectionSet, v models.SearchResult) graphql.Marshaler { - return ec._SearchResult(ctx, sel, &v) + if v == nil { + if !ec.HasError(graphql.GetResolverContext(ctx)) { + ec.Errorf(ctx, "must not be null") + } + return graphql.Null + } + return ec._SearchResult(ctx, sel, v) } func (ec *executionContext) marshalNSearchResult2ᚕgithubᚗcomᚋ99designsᚋgqlgenᚋexampleᚋstarwarsᚋmodelsᚐSearchResult(ctx context.Context, sel ast.SelectionSet, v []models.SearchResult) graphql.Marshaler { @@ -5002,7 +5014,10 @@ func (ec *executionContext) marshalOBoolean2ᚖbool(ctx context.Context, sel ast } func (ec *executionContext) marshalOCharacter2githubᚗcomᚋ99designsᚋgqlgenᚋexampleᚋstarwarsᚋmodelsᚐCharacter(ctx context.Context, sel ast.SelectionSet, v models.Character) graphql.Marshaler { - return ec._Character(ctx, sel, &v) + if v == nil { + return graphql.Null + } + return ec._Character(ctx, sel, v) } func (ec *executionContext) marshalOCharacter2ᚕgithubᚗcomᚋ99designsᚋgqlgenᚋexampleᚋstarwarsᚋmodelsᚐCharacter(ctx context.Context, sel ast.SelectionSet, v []models.Character) graphql.Marshaler { diff --git a/example/type-system-extension/generated.go b/example/type-system-extension/generated.go index b4597e2f3da..a490de20442 100644 --- a/example/type-system-extension/generated.go +++ b/example/type-system-extension/generated.go @@ -1914,8 +1914,8 @@ func (ec *executionContext) unmarshalInputTodoInput(ctx context.Context, obj int // region ************************** interface.gotpl *************************** -func (ec *executionContext) _Data(ctx context.Context, sel ast.SelectionSet, obj *Data) graphql.Marshaler { - switch obj := (*obj).(type) { +func (ec *executionContext) _Data(ctx context.Context, sel ast.SelectionSet, obj Data) graphql.Marshaler { + switch obj := (obj).(type) { case nil: return graphql.Null case Todo: @@ -1927,8 +1927,8 @@ func (ec *executionContext) _Data(ctx context.Context, sel ast.SelectionSet, obj } } -func (ec *executionContext) _Node(ctx context.Context, sel ast.SelectionSet, obj *Node) graphql.Marshaler { - switch obj := (*obj).(type) { +func (ec *executionContext) _Node(ctx context.Context, sel ast.SelectionSet, obj Node) graphql.Marshaler { + switch obj := (obj).(type) { case nil: return graphql.Null case Todo: