Skip to content

Commit

Permalink
Fix retrieving selected fields in ApplyFragments (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
edwinmo-splunk authored and AlecAivazis committed Aug 23, 2019
1 parent 2f3bbfb commit d8a3582
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions language.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ type CollectedField struct {
// CollectedFieldList is a list of CollectedField with utilities for retrieving them
type CollectedFieldList []*CollectedField

func getDisplayName(field *ast.Field) string {
if field.Alias != "" {
return field.Alias
}
return field.Name
}

func (c *CollectedFieldList) GetOrCreateForAlias(alias string, creator func() *CollectedField) *CollectedField {
// look for the field with the given alias
for _, field := range *c {
Expand Down Expand Up @@ -75,7 +82,7 @@ func collectFields(sources []ast.SelectionSet, fragments ast.FragmentDefinitionL
}

// look up the entry in the field list for this field
collected := selectedFields.GetOrCreateForAlias(selectedField.Alias, func() *CollectedField {
collected := selectedFields.GetOrCreateForAlias(getDisplayName(selectedField), func() *CollectedField {
return &CollectedField{Field: selectedField}
})

Expand Down Expand Up @@ -111,7 +118,7 @@ func collectFields(sources []ast.SelectionSet, fragments ast.FragmentDefinitionL
// each field in the inline fragment needs to be added to the selection
for _, fragmentSelection := range *fields {
// add the selection from the field to our accumulator
collected := selectedFields.GetOrCreateForAlias(fragmentSelection.Alias, func() *CollectedField {
collected := selectedFields.GetOrCreateForAlias(getDisplayName(fragmentSelection.Field), func() *CollectedField {
return fragmentSelection
})

Expand Down

0 comments on commit d8a3582

Please sign in to comment.