Skip to content

Commit

Permalink
We should just use lock, no need the premature optimization.
Browse files Browse the repository at this point in the history
  • Loading branch information
bigdrum committed Jul 24, 2016
1 parent fd2a5d6 commit d514566
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"reflect"
"strings"
"sync"

"github.com/graphql-go/graphql/gqlerrors"
"github.com/graphql-go/graphql/language/ast"
Expand Down Expand Up @@ -271,26 +272,21 @@ func executeFields(p ExecuteFieldsParams) *Result {

finalResults := map[string]interface{}{}
fs := make([]func(), 0, len(p.Fields))
var undefinedKeys []string
var resultsMu sync.Mutex
for responseName, fieldASTs := range p.Fields {
responseName := responseName
fieldASTs := fieldASTs
finalResults[responseName] = nil // This is to avoid using lock.
fs = append(fs, func() {
resolved, state := resolveField(p.ExecutionContext, p.ParentType, p.Source, fieldASTs)
if state.hasNoFieldDefs {
undefinedKeys = append(undefinedKeys, responseName)
return
}
resultsMu.Lock()
finalResults[responseName] = resolved
resultsMu.Unlock()
})
}
p.ExecutionContext.Executor.RunMany(fs)

// Remove undefined keys.
for _, key := range undefinedKeys {
delete(finalResults, key)
}
return &Result{
Data: finalResults,
Errors: p.ExecutionContext.Errors,
Expand Down

0 comments on commit d514566

Please sign in to comment.