Skip to content

Commit

Permalink
Merge 15f000b into 3e619b6
Browse files Browse the repository at this point in the history
  • Loading branch information
ssttevee committed Jul 12, 2017
2 parents 3e619b6 + 15f000b commit 8a6d239
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 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 @@ -61,7 +62,7 @@ func Execute(p ExecuteParams) (result *Result) {
if r, ok := r.(error); ok {
err = gqlerrors.FormatError(r)
}
exeContext.Errors = append(exeContext.Errors, gqlerrors.FormatError(err))
exeContext.addError(gqlerrors.FormatError(err))
result.Errors = exeContext.Errors
select {
case out <- result:
Expand Down Expand Up @@ -110,6 +111,14 @@ type ExecutionContext struct {
VariableValues map[string]interface{}
Errors []gqlerrors.FormattedError
Context context.Context

errorsMutex sync.Mutex
}

func (eCtx *ExecutionContext) addError(err gqlerrors.FormattedError) {
eCtx.errorsMutex.Lock()
eCtx.Errors = append(eCtx.Errors, err)
eCtx.errorsMutex.Unlock()
}

func buildExecutionContext(p BuildExecutionCtxParams) (*ExecutionContext, error) {
Expand Down Expand Up @@ -278,13 +287,29 @@ func executeFields(p ExecuteFieldsParams) *Result {
p.Fields = map[string][]*ast.Field{}
}

recoverChan := make(chan interface{}, len(p.Fields))

var resultsMutex sync.Mutex
finalResults := map[string]interface{}{}
for responseName, fieldASTs := range p.Fields {
resolved, state := resolveField(p.ExecutionContext, p.ParentType, p.Source, fieldASTs)
if state.hasNoFieldDefs {
continue
go func(responseName string, fieldASTs []*ast.Field) {
defer func() {
recoverChan <- recover()
}()
resolved, state := resolveField(p.ExecutionContext, p.ParentType, p.Source, fieldASTs)
if state.hasNoFieldDefs {
return
}
resultsMutex.Lock()
finalResults[responseName] = resolved
resultsMutex.Unlock()
}(responseName, fieldASTs)
}

for range p.Fields {
if r := <-recoverChan; r != nil {
panic(r)
}
finalResults[responseName] = resolved
}

return &Result{
Expand Down Expand Up @@ -527,7 +552,7 @@ func resolveField(eCtx *ExecutionContext, parentType *Object, source interface{}
if _, ok := returnType.(*NonNull); ok {
panic(gqlerrors.FormatError(err))
}
eCtx.Errors = append(eCtx.Errors, gqlerrors.FormatError(err))
eCtx.addError(gqlerrors.FormatError(err))
return result, resultState
}
return result, resultState
Expand Down Expand Up @@ -593,7 +618,7 @@ func completeValueCatchingError(eCtx *ExecutionContext, returnType Type, fieldAS
panic(r)
}
if err, ok := r.(gqlerrors.FormattedError); ok {
eCtx.Errors = append(eCtx.Errors, err)
eCtx.addError(err)
}
return completed
}
Expand Down

0 comments on commit 8a6d239

Please sign in to comment.