Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix field merging behavior for fragments on interfaces #2380

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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
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)
})
}