Skip to content

Commit

Permalink
fix field merging behavior for fragments on interfaces (#2380)
Browse files Browse the repository at this point in the history
Co-authored-by: Kevin Fang <kevin@sorare.com>
  • Loading branch information
kxfang and Kevin Fang authored Sep 17, 2022
1 parent 6bb3186 commit d29d098
Show file tree
Hide file tree
Showing 15 changed files with 847 additions and 0 deletions.
242 changes: 242 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.

9 changes: 9 additions & 0 deletions codegen/testserver/followschema/interfaces.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@ extend type Query {
noShapeTypedNil: Shape @makeTypedNil
animal: Animal @makeTypedNil
notAnInterface: BackedByInterface
dog: Dog
}

interface Animal {
species: String!
size: Size!
}

type Size {
height: Int!
weight: Int!
}

type BackedByInterface {
Expand All @@ -19,11 +26,13 @@ type BackedByInterface {

type Dog implements Animal {
species: String!
size: Size!
dogBreed: String!
}

type Cat implements Animal {
species: String!
size: Size!
catBreed: String!
}

Expand Down
41 changes: 41 additions & 0 deletions codegen/testserver/followschema/interfaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,45 @@ func TestInterfaces(t *testing.T) {
require.Equal(t, float64(1), resp.Shapes[1].Coordinates.X)
require.Equal(t, float64(1), resp.Shapes[1].Coordinates.Y)
})

t.Run("fragment on interface must return merged fields", func(t *testing.T) {
resolvers := &Stub{}
resolvers.QueryResolver.Dog = func(ctx context.Context) (dog *Dog, err error) {
return &Dog{
Size: &Size{
Height: 100,
Weight: 35,
},
}, nil
}

c := client.New(handler.NewDefaultServer(NewExecutableSchema(Config{Resolvers: resolvers})))
var resp struct {
Dog struct {
Size struct {
Height int
Weight int
}
}
}

c.MustPost(`
{
dog {
size {
height
}
...AnimalWeight
}
}
fragment AnimalWeight on Animal {
size {
weight
}
}
`, &resp)

require.Equal(t, 100, resp.Dog.Size.Height)
require.Equal(t, 35, resp.Dog.Size.Weight)
})
}
Loading

0 comments on commit d29d098

Please sign in to comment.