diff --git a/codegen/interface_build.go b/codegen/interface_build.go index 672a9e82bf..de8541cb2d 100644 --- a/codegen/interface_build.go +++ b/codegen/interface_build.go @@ -37,7 +37,11 @@ func buildInterface(types NamedTypes, typ schema.NamedType, prog *loader.Program for _, implementor := range typ.PossibleTypes { t := types[implementor.TypeName()] - i.Implementors = append(i.Implementors, InterfaceImplementor{NamedType: t, ValueReceiver: true}) + + i.Implementors = append(i.Implementors, InterfaceImplementor{ + NamedType: t, + ValueReceiver: isValueReceiver(types[typ.Name], t, prog), + }) } return i diff --git a/test/generated.go b/test/generated.go index 1f61592891..3f84a4ce6d 100644 --- a/test/generated.go +++ b/test/generated.go @@ -876,6 +876,19 @@ func (ec *executionContext) _Shape(sel []query.Selection, obj *Shape) graphql.Ma } } +func (ec *executionContext) _ShapeUnion(sel []query.Selection, obj *ShapeUnion) graphql.Marshaler { + switch obj := (*obj).(type) { + case nil: + return graphql.Null + case *Circle: + return ec._Circle(sel, obj) + case *Rectangle: + return ec._Rectangle(sel, obj) + default: + panic(fmt.Errorf("unexpected type %T", obj)) + } +} + func UnmarshalInnerInput(v interface{}) (InnerInput, error) { var it InnerInput @@ -912,7 +925,7 @@ func UnmarshalOuterInput(v interface{}) (OuterInput, error) { return it, nil } -var parsedSchema = schema.MustParse("input InnerInput {\n id:Int!\n}\n\ninput OuterInput {\n inner: InnerInput!\n}\n\ntype OuterObject {\n inner: InnerObject!\n}\n\ntype InnerObject {\n id: Int!\n}\n\ninterface Shape {\n area: Float\n}\n\ntype Circle implements Shape {\n radius: Float\n area: Float\n}\n\ntype Rectangle implements Shape {\n length: Float\n width: Float\n area: Float\n}\n\ntype Query {\n nestedInputs(input: [[OuterInput]] = [[{inner: {id: 1}}]]): Boolean\n nestedOutputs: [[OuterObject]]\n shapes: [Shape]\n}\n") +var parsedSchema = schema.MustParse("input InnerInput {\n id:Int!\n}\n\ninput OuterInput {\n inner: InnerInput!\n}\n\ntype OuterObject {\n inner: InnerObject!\n}\n\ntype InnerObject {\n id: Int!\n}\n\ninterface Shape {\n area: Float\n}\n\ntype Circle implements Shape {\n radius: Float\n area: Float\n}\n\ntype Rectangle implements Shape {\n length: Float\n width: Float\n area: Float\n}\n\nunion ShapeUnion = Circle | Rectangle\n\ntype Query {\n nestedInputs(input: [[OuterInput]] = [[{inner: {id: 1}}]]): Boolean\n nestedOutputs: [[OuterObject]]\n shapes: [Shape]\n}\n") func (ec *executionContext) introspectSchema() *introspection.Schema { return introspection.WrapSchema(parsedSchema) diff --git a/test/models.go b/test/models.go index 9777fa5e0a..56685be145 100644 --- a/test/models.go +++ b/test/models.go @@ -6,6 +6,10 @@ type Shape interface { Area() float64 } +type ShapeUnion interface { + Area() float64 +} + type Circle struct { Radius float64 } diff --git a/test/schema.graphql b/test/schema.graphql index 2e8a4d0f36..9ddf882b97 100644 --- a/test/schema.graphql +++ b/test/schema.graphql @@ -29,6 +29,8 @@ type Rectangle implements Shape { area: Float } +union ShapeUnion = Circle | Rectangle + type Query { nestedInputs(input: [[OuterInput]] = [[{inner: {id: 1}}]]): Boolean nestedOutputs: [[OuterObject]] diff --git a/test/types.json b/test/types.json index 251fa29d8b..474a4e7c77 100644 --- a/test/types.json +++ b/test/types.json @@ -1,5 +1,6 @@ { "Shape": "github.com/vektah/gqlgen/test.Shape", + "ShapeUnion": "github.com/vektah/gqlgen/test.ShapeUnion", "Circle": "github.com/vektah/gqlgen/test.Circle", "Rectangle": "github.com/vektah/gqlgen/test.Rectangle" }