From 188c1c1dca68b7b855d2a7e5079b47928f2c4a18 Mon Sep 17 00:00:00 2001 From: Greg MacWilliam Date: Tue, 9 Nov 2021 09:13:47 -0500 Subject: [PATCH] add tests. --- execution_test.go | 49 ++++++++++++++++++++++++++++++++++++++++++++++ query_execution.go | 2 +- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/execution_test.go b/execution_test.go index 938ec9e2..4328b159 100644 --- a/execution_test.go +++ b/execution_test.go @@ -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{ diff --git a/query_execution.go b/query_execution.go index d71bb52b..0f15f50c 100644 --- a/query_execution.go +++ b/query_execution.go @@ -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 {