-
Notifications
You must be signed in to change notification settings - Fork 834
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Experimental implementation of concurrently resolving fields
- Loading branch information
Showing
1 changed file
with
41 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -274,15 +274,51 @@ func executeFields(p ExecuteFieldsParams) *Result { | |
p.Fields = map[string][]*ast.Field{} | ||
} | ||
|
||
finalResults := map[string]interface{}{} | ||
type resolvedChanResult struct { | ||
responseName string | ||
resolved interface{} | ||
state resolveFieldResultState | ||
panicErr error | ||
} | ||
|
||
// concurrently resolve fields | ||
var wg sync.WaitGroup | ||
resolvedChan := make(chan resolvedChanResult, len(p.Fields)) | ||
for responseName, fieldASTs := range p.Fields { | ||
resolved, state := resolveField(p.ExecutionContext, p.ParentType, p.Source, fieldASTs) | ||
if state.hasNoFieldDefs { | ||
wg.Add(1) | ||
go func(responseName string, fieldASTs []*ast.Field) (resolved interface{}, state resolveFieldResultState, panicErr error) { | ||
This comment has been minimized.
Sorry, something went wrong. |
||
defer func() { | ||
if r := recover(); r != nil { | ||
if r, ok := r.(error); ok { | ||
This comment has been minimized.
Sorry, something went wrong.
andreas
|
||
// recover panic that was thrown by resolveFields(), | ||
// indicating that we should short-circuit traversal chain later | ||
panicErr = r | ||
} | ||
} | ||
resolvedChan <- resolvedChanResult{responseName, resolved, state, panicErr} | ||
wg.Done() | ||
}() | ||
resolved, state = resolveField(p.ExecutionContext, p.ParentType, p.Source, fieldASTs) | ||
return resolved, state, panicErr | ||
}(responseName, fieldASTs) | ||
} | ||
|
||
// wait for all routines to complete and then perform clean up | ||
wg.Wait() | ||
close(resolvedChan) | ||
|
||
// iterate through resolved results | ||
finalResults := map[string]interface{}{} | ||
for result := range resolvedChan { | ||
if result.panicErr != nil { | ||
// short-circuit field resolution traversal chain | ||
panic(result.panicErr) | ||
} | ||
if result.state.hasNoFieldDefs { | ||
continue | ||
} | ||
finalResults[responseName] = resolved | ||
finalResults[result.responseName] = result.resolved | ||
} | ||
|
||
return &Result{ | ||
Data: finalResults, | ||
Errors: p.ExecutionContext.Errors(), | ||
|
Could you omit the return values of the function, i.e.
func(responseName, string, fieldASTs []*ast.Field) { ... }
? As far as I can tell they are not used as the result is communicated viaresolvedChan
.