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

Rajas/fix gqlgen 1299 #1341

Merged
merged 2 commits into from
Sep 28, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions graphql/context_operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,31 @@ func TestCollectAllFields(t *testing.T) {
require.NotEqual(t, collected[0], collected[1])
require.Equal(t, collected[0].Name, collected[1].Name)
})

t.Run("collect fragments with same field name and different alias", func(t *testing.T) {
ctx := testContext(ast.SelectionSet{
&ast.InlineFragment{
TypeCondition: "ExampleTypeA",
SelectionSet: ast.SelectionSet{
&ast.Field{
Name: "fieldA",
Alias: "fieldA",
ObjectDefinition: &ast.Definition{Name: "ExampleTypeA"},
},
&ast.Field{
Name: "fieldA",
Alias: "fieldA Alias",
ObjectDefinition: &ast.Definition{Name: "ExampleTypeA"},
},
},
ObjectDefinition: &ast.Definition{Name: "ExampleType", Kind: ast.Interface},
},
})
resCtx := GetFieldContext(ctx)
collected := CollectFields(GetOperationContext(ctx), resCtx.Field.Selections, nil)
require.Len(t, collected, 2)
require.NotEqual(t, collected[0], collected[1])
require.Equal(t, collected[0].Name, collected[1].Name)
require.NotEqual(t, collected[0].Alias, collected[1].Alias)
})
}
10 changes: 5 additions & 5 deletions graphql/executable_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func collectFields(reqCtx *OperationContext, selSet ast.SelectionSet, satisfies
if !shouldIncludeNode(sel.Directives, reqCtx.Variables) {
continue
}
f := getOrCreateAndAppendField(&groupedFields, sel.Alias, sel.ObjectDefinition, func() CollectedField {
f := getOrCreateAndAppendField(&groupedFields, sel.Name, sel.Alias, sel.ObjectDefinition, func() CollectedField {
return CollectedField{Field: sel}
})

Expand All @@ -45,7 +45,7 @@ func collectFields(reqCtx *OperationContext, selSet ast.SelectionSet, satisfies
continue
}
for _, childField := range collectFields(reqCtx, sel.SelectionSet, satisfies, visited) {
f := getOrCreateAndAppendField(&groupedFields, childField.Name, childField.ObjectDefinition, func() CollectedField { return childField })
f := getOrCreateAndAppendField(&groupedFields, childField.Name, childField.Alias, childField.ObjectDefinition, func() CollectedField { return childField })
f.Selections = append(f.Selections, childField.Selections...)
}

Expand All @@ -70,7 +70,7 @@ func collectFields(reqCtx *OperationContext, selSet ast.SelectionSet, satisfies
}

for _, childField := range collectFields(reqCtx, fragment.SelectionSet, satisfies, visited) {
f := getOrCreateAndAppendField(&groupedFields, childField.Name, childField.ObjectDefinition, func() CollectedField { return childField })
f := getOrCreateAndAppendField(&groupedFields, childField.Name, childField.Alias, childField.ObjectDefinition, func() CollectedField { return childField })
f.Selections = append(f.Selections, childField.Selections...)
}
default:
Expand All @@ -96,9 +96,9 @@ func instanceOf(val string, satisfies []string) bool {
return false
}

func getOrCreateAndAppendField(c *[]CollectedField, name string, objectDefinition *ast.Definition, creator func() CollectedField) *CollectedField {
func getOrCreateAndAppendField(c *[]CollectedField, name string, alias string, objectDefinition *ast.Definition, creator func() CollectedField) *CollectedField {
for i, cf := range *c {
if cf.Alias == name && (cf.ObjectDefinition == objectDefinition || (cf.ObjectDefinition != nil && objectDefinition != nil && cf.ObjectDefinition.Name == objectDefinition.Name)) {
if cf.Name == name && cf.Alias == alias && (cf.ObjectDefinition == objectDefinition || (cf.ObjectDefinition != nil && objectDefinition != nil && cf.ObjectDefinition.Name == objectDefinition.Name)) {
return &(*c)[i]
}
}
Expand Down