Skip to content

Commit

Permalink
add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
gmac committed Nov 9, 2021
1 parent d7123c6 commit 188c1c1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
49 changes: 49 additions & 0 deletions execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2233,6 +2233,55 @@ func TestUnionAndTrimSelectionSet(t *testing.T) {
require.Equal(t, selectionSet, filtered)
})

t.Run("removes duplicate leaf values and merges composite scopes", func(t *testing.T) {
selectionSet := ast.SelectionSet{
&ast.Field{
Alias: "name",
Name: "name",
Definition: schema.Types["Agent"].Fields.ForName("name"),
ObjectDefinition: schema.Types["Agent"],
},
&ast.Field{
Alias: "name",
Name: "name",
Definition: schema.Types["Agent"].Fields.ForName("name"),
ObjectDefinition: schema.Types["Agent"],
},
&ast.Field{
Alias: "country",
Name: "country",
Definition: schema.Types["Agent"].Fields.ForName("country"),
ObjectDefinition: schema.Types["Agent"],
SelectionSet: []ast.Selection{
&ast.Field{
Alias: "id",
Name: "id",
Definition: schema.Types["Country"].Fields.ForName("id"),
ObjectDefinition: schema.Types["Country"],
},
},
},
&ast.Field{
Alias: "country",
Name: "country",
Definition: schema.Types["Agent"].Fields.ForName("country"),
ObjectDefinition: schema.Types["Agent"],
SelectionSet: []ast.Selection{
&ast.Field{
Alias: "name",
Name: "name",
Definition: schema.Types["Country"].Fields.ForName("name"),
ObjectDefinition: schema.Types["Country"],
},
},
},
}

filtered, err := unionAndTrimSelectionSet("", schema, selectionSet)
require.NoError(t, err)
require.Equal(t, formatSelectionSetSingleLine(ctx, schema, filtered), "{ name country { id name } }")
})

t.Run("removes field duplicates from inline fragment", func(t *testing.T) {
initialSelectionSet := ast.SelectionSet{
&ast.Field{
Expand Down
2 changes: 1 addition & 1 deletion query_execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,7 @@ func unionAndTrimSelectionSetRec(objectTypename string, schema *ast.Schema, sele
switch selection := selection.(type) {
case *ast.Field:
if seenField, ok := seenFields[selection.Alias]; ok {
if seenField.SelectionSet != nil && selection.SelectionSet != nil {
if seenField.Name == selection.Name && seenField.SelectionSet != nil && selection.SelectionSet != nil {
seenField.SelectionSet = append(seenField.SelectionSet, selection.SelectionSet...)
}
} else {
Expand Down

0 comments on commit 188c1c1

Please sign in to comment.