Skip to content

Commit

Permalink
Merge pull request #493 from bhoriuchi/master
Browse files Browse the repository at this point in the history
adds a field sort by source location before executing serially
  • Loading branch information
chris-ramon authored Feb 5, 2020
2 parents d925012 + b6b24cf commit 59637ea
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"reflect"
"sort"
"strings"

"github.com/graphql-go/graphql/gqlerrors"
Expand Down Expand Up @@ -254,7 +255,9 @@ func executeFieldsSerially(p executeFieldsParams) *Result {
}

finalResults := make(map[string]interface{}, len(p.Fields))
for responseName, fieldASTs := range p.Fields {
for _, orderedField := range orderedFields(p.Fields) {
responseName := orderedField.responseName
fieldASTs := orderedField.fieldASTs
fieldPath := p.Path.WithKey(responseName)
resolved, state := resolveField(p.ExecutionContext, p.ParentType, p.Source, fieldASTs, fieldPath)
if state.hasNoFieldDefs {
Expand Down Expand Up @@ -1038,3 +1041,39 @@ func getFieldDef(schema Schema, parentType *Object, fieldName string) *FieldDefi
}
return parentType.Fields()[fieldName]
}

// contains field information that will be placed in an ordered slice
type orderedField struct {
responseName string
fieldASTs []*ast.Field
}

// orders fields from a fields map by location in the source
func orderedFields(fields map[string][]*ast.Field) []*orderedField {
orderedFields := []*orderedField{}
fieldMap := map[int]*orderedField{}
startLocs := []int{}

for responseName, fieldASTs := range fields {
// find the lowest location in the current fieldASTs
lowest := -1
for _, fieldAST := range fieldASTs {
loc := fieldAST.GetLoc().Start
if lowest == -1 || loc < lowest {
lowest = loc
}
}
startLocs = append(startLocs, lowest)
fieldMap[lowest] = &orderedField{
responseName: responseName,
fieldASTs: fieldASTs,
}
}

sort.Ints(startLocs)
for _, startLoc := range startLocs {
orderedFields = append(orderedFields, fieldMap[startLoc])
}

return orderedFields
}

0 comments on commit 59637ea

Please sign in to comment.