From 15b3af2d5a6ed7240d09fb4c21fd905c8c316aa7 Mon Sep 17 00:00:00 2001 From: Adam Scarr Date: Sun, 18 Mar 2018 09:08:48 +1100 Subject: [PATCH] Fix value receivers for unions too fixes 42 --- codegen/interface_build.go | 6 +++++- test/generated.go | 15 ++++++++++++++- test/models.go | 4 ++++ test/schema.graphql | 2 ++ test/types.json | 1 + 5 files changed, 26 insertions(+), 2 deletions(-) diff --git a/codegen/interface_build.go b/codegen/interface_build.go index 672a9e82bff..de8541cb2d9 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 1f61592891b..3f84a4ce6d3 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 9777fa5e0a6..56685be145b 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 2e8a4d0f36a..9ddf882b97d 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 251fa29d8b9..474a4e7c778 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" }