Skip to content

Commit

Permalink
Fix value receivers for unions too
Browse files Browse the repository at this point in the history
fixes 42
  • Loading branch information
vektah committed Mar 17, 2018
1 parent 46103bd commit 15b3af2
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 2 deletions.
6 changes: 5 additions & 1 deletion codegen/interface_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 14 additions & 1 deletion test/generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions test/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ type Shape interface {
Area() float64
}

type ShapeUnion interface {
Area() float64
}

type Circle struct {
Radius float64
}
Expand Down
2 changes: 2 additions & 0 deletions test/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand Down
1 change: 1 addition & 0 deletions test/types.json
Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit 15b3af2

Please sign in to comment.