Skip to content

Commit

Permalink
Merge Inline Fragment Nested Interface Fields (#1663)
Browse files Browse the repository at this point in the history
  • Loading branch information
wilhelmeek authored Oct 16, 2021
1 parent 8b97371 commit db4b5eb
Show file tree
Hide file tree
Showing 13 changed files with 660 additions and 20 deletions.
181 changes: 181 additions & 0 deletions codegen/testserver/followschema/interfaces.generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions codegen/testserver/followschema/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type ShapeUnion interface {

type Circle struct {
Radius float64
Coordinates
}

func (c *Circle) Area() float64 {
Expand All @@ -25,6 +26,7 @@ func (c *Circle) isShape() {}

type Rectangle struct {
Length, Width float64
Coordinates
}

func (r *Rectangle) Area() float64 {
Expand Down Expand Up @@ -79,6 +81,7 @@ type BackedByInterfaceImpl struct {
func (b *BackedByInterfaceImpl) ThisShouldBind() string {
return b.Value
}

func (b *BackedByInterfaceImpl) ThisShouldBindWithError() (string, error) {
return b.Value, b.Error
}
12 changes: 10 additions & 2 deletions codegen/testserver/followschema/interfaces.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,27 @@ type Cat implements Animal {
catBreed: String!
}

type Coordinates {
x: Float!
y: Float!
}
interface Shape {
area: Float
coordinates: Coordinates
}

type Circle implements Shape {
radius: Float
area: Float
coordinates: Coordinates
}
type Rectangle implements Shape {
length: Float
width: Float
area: Float
coordinates: Coordinates
}
union ShapeUnion @goModel(model:"followschema.ShapeUnion") = Circle | Rectangle
union ShapeUnion @goModel(model: "followschema.ShapeUnion") = Circle | Rectangle

directive @makeNil on FIELD_DEFINITION
directive @makeTypedNil on FIELD_DEFINITION
Expand All @@ -55,7 +63,7 @@ type ConcreteNodeA implements Node {
name: String!
}

""" Implements the Node interface with another interface """
" Implements the Node interface with another interface "
type ConcreteNodeInterface implements Node {
id: ID!
child: Node!
Expand Down
56 changes: 56 additions & 0 deletions codegen/testserver/followschema/interfaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,60 @@ func TestInterfaces(t *testing.T) {
require.Equal(t, "CNII", resp.Node.ID)
require.Equal(t, "Child", resp.Node.Child.ID)
})

t.Run("interface implementors should return merged base fields", func(t *testing.T) {
resolvers := &Stub{}
resolvers.QueryResolver.Shapes = func(ctx context.Context) (shapes []Shape, err error) {
return []Shape{
&Rectangle{
Coordinates: Coordinates{
X: -1,
Y: -1,
},
},
&Circle{
Coordinates: Coordinates{
X: 1,
Y: 1,
},
},
}, nil
}

c := client.New(handler.NewDefaultServer(NewExecutableSchema(Config{Resolvers: resolvers})))
var resp struct {
Shapes []struct {
Coordinates struct {
X float64
Y float64
}
}
}

c.MustPost(`
{
shapes {
coordinates {
x
}
... on Rectangle {
coordinates {
x
}
}
... on Circle {
coordinates {
y
}
}
}
}
`, &resp)

require.Equal(t, 2, len(resp.Shapes))
require.Equal(t, float64(-1), resp.Shapes[0].Coordinates.X)
require.Equal(t, float64(0), resp.Shapes[0].Coordinates.Y)
require.Equal(t, float64(1), resp.Shapes[1].Coordinates.X)
require.Equal(t, float64(1), resp.Shapes[1].Coordinates.Y)
})
}
5 changes: 5 additions & 0 deletions codegen/testserver/followschema/models-gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions codegen/testserver/followschema/prelude.generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit db4b5eb

Please sign in to comment.