- Notifications
You must be signed in to change notification settings - Fork 2.1k
Execute: simplify + speedup #1286
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits Select commit Hold shift + click to select a range
2daa853 Remove 'completeValueWithLocatedError'
IvanGoncharov de51665 Execute: move promise resolution to the completeValueCatchingError
IvanGoncharov 7496e73 Remove fat arrow function
IvanGoncharov b364898 Execute: rewrite executeFields to use for-in instead of reduce
IvanGoncharov c52afaa Extract locatedFieldError function
IvanGoncharov 3802af7 extract handleFieldError function
IvanGoncharov c4fe200 Address @leebyron review comments
IvanGoncharov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| | @@ -506,9 +506,11 @@ function executeFields( | |||||||||||||||
| path: ResponsePath | void, | ||||||||||||||||
| fields: ObjMap<Array<FieldNode>>, | ||||||||||||||||
| ): MaybePromise<ObjMap<mixed>> { | ||||||||||||||||
| const results = Object.create(null); | ||||||||||||||||
| let containsPromise = false; | ||||||||||||||||
| | ||||||||||||||||
| const finalResults = Object.keys(fields).reduce((results, responseName) => { | ||||||||||||||||
| for (let i = 0, keys = Object.keys(fields); i < keys.length; ++i) { | ||||||||||||||||
| const responseName = keys[i]; | ||||||||||||||||
| const fieldNodes = fields[responseName]; | ||||||||||||||||
| const fieldPath = addPath(path, responseName); | ||||||||||||||||
| const result = resolveField( | ||||||||||||||||
| | @@ -518,26 +520,24 @@ function executeFields( | |||||||||||||||
| fieldNodes, | ||||||||||||||||
| fieldPath, | ||||||||||||||||
| ); | ||||||||||||||||
| if (result === undefined) { | ||||||||||||||||
| return results; | ||||||||||||||||
| } | ||||||||||||||||
| results[responseName] = result; | ||||||||||||||||
| if (!containsPromise && isPromise(result)) { | ||||||||||||||||
| containsPromise = true; | ||||||||||||||||
| | ||||||||||||||||
| if (result !== undefined) { | ||||||||||||||||
| results[responseName] = result; | ||||||||||||||||
| if (!containsPromise && isPromise(result)) { | ||||||||||||||||
| containsPromise = true; | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
| return results; | ||||||||||||||||
| }, Object.create(null)); | ||||||||||||||||
| } | ||||||||||||||||
| | ||||||||||||||||
| // If there are no promises, we can just return the object | ||||||||||||||||
| if (!containsPromise) { | ||||||||||||||||
| return finalResults; | ||||||||||||||||
| return results; | ||||||||||||||||
| } | ||||||||||||||||
| | ||||||||||||||||
| // Otherwise, results is a map from field name to the result | ||||||||||||||||
| // of resolving that field, which is possibly a promise. Return | ||||||||||||||||
| // a promise that will return this same map, but with any | ||||||||||||||||
| // promises replaced with the values they resolved to. | ||||||||||||||||
| return promiseForObject(finalResults); | ||||||||||||||||
| // Otherwise, results is a map from field name to the result of resolving that | ||||||||||||||||
| // field, which is possibly a promise. Return a promise that will return this | ||||||||||||||||
| // same map, but with any promises replaced with the values they resolved to. | ||||||||||||||||
| return promiseForObject(results); | ||||||||||||||||
| } | ||||||||||||||||
| | ||||||||||||||||
| /** | ||||||||||||||||
| | @@ -792,87 +792,53 @@ function completeValueCatchingError( | |||||||||||||||
| path: ResponsePath, | ||||||||||||||||
| result: mixed, | ||||||||||||||||
| ): MaybePromise<mixed> { | ||||||||||||||||
| // If the field type is non-nullable, then it is resolved without any | ||||||||||||||||
| // protection from errors, however it still properly locates the error. | ||||||||||||||||
| if (isNonNullType(returnType)) { | ||||||||||||||||
| return completeValueWithLocatedError( | ||||||||||||||||
| exeContext, | ||||||||||||||||
| returnType, | ||||||||||||||||
| fieldNodes, | ||||||||||||||||
| info, | ||||||||||||||||
| path, | ||||||||||||||||
| result, | ||||||||||||||||
| ); | ||||||||||||||||
| } | ||||||||||||||||
| | ||||||||||||||||
| // Otherwise, error protection is applied, logging the error and resolving | ||||||||||||||||
| // a null value for this field if one is encountered. | ||||||||||||||||
| try { | ||||||||||||||||
| const completed = completeValueWithLocatedError( | ||||||||||||||||
| exeContext, | ||||||||||||||||
| returnType, | ||||||||||||||||
| fieldNodes, | ||||||||||||||||
| info, | ||||||||||||||||
| path, | ||||||||||||||||
| result, | ||||||||||||||||
| ); | ||||||||||||||||
| let completed; | ||||||||||||||||
| if (isPromise(result)) { | ||||||||||||||||
| completed = result.then(resolved => | ||||||||||||||||
| completeValue(exeContext, returnType, fieldNodes, info, path, resolved), | ||||||||||||||||
| ); | ||||||||||||||||
| } else { | ||||||||||||||||
| completed = completeValue( | ||||||||||||||||
| exeContext, | ||||||||||||||||
| returnType, | ||||||||||||||||
| fieldNodes, | ||||||||||||||||
| info, | ||||||||||||||||
| path, | ||||||||||||||||
| result, | ||||||||||||||||
| ); | ||||||||||||||||
| } | ||||||||||||||||
| | ||||||||||||||||
| if (isPromise(completed)) { | ||||||||||||||||
| // If `completeValueWithLocatedError` returned a rejected promise, log | ||||||||||||||||
| // the rejection error and resolve to null. | ||||||||||||||||
| // Note: we don't rely on a `catch` method, but we do expect "thenable" | ||||||||||||||||
| // to take a second callback for the error case. | ||||||||||||||||
| return completed.then(undefined, error => { | ||||||||||||||||
| exeContext.errors.push(error); | ||||||||||||||||
| return Promise.resolve(null); | ||||||||||||||||
| }); | ||||||||||||||||
| return completed.then(undefined, error => | ||||||||||||||||
| handleFieldError(error, fieldNodes, path, returnType, exeContext), | ||||||||||||||||
| ); | ||||||||||||||||
| } | ||||||||||||||||
| return completed; | ||||||||||||||||
| } catch (error) { | ||||||||||||||||
| // If `completeValueWithLocatedError` returned abruptly (threw an error), | ||||||||||||||||
| // log the error and return null. | ||||||||||||||||
| exeContext.errors.push(error); | ||||||||||||||||
| return null; | ||||||||||||||||
| return handleFieldError(error, fieldNodes, path, returnType, exeContext); | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
| | ||||||||||||||||
| // This is a small wrapper around completeValue which annotates errors with | ||||||||||||||||
| // location information. | ||||||||||||||||
| function completeValueWithLocatedError( | ||||||||||||||||
| exeContext: ExecutionContext, | ||||||||||||||||
| returnType: GraphQLOutputType, | ||||||||||||||||
| fieldNodes: $ReadOnlyArray<FieldNode>, | ||||||||||||||||
| info: GraphQLResolveInfo, | ||||||||||||||||
| path: ResponsePath, | ||||||||||||||||
| result: mixed, | ||||||||||||||||
| ): MaybePromise<mixed> { | ||||||||||||||||
| try { | ||||||||||||||||
| const completed = completeValue( | ||||||||||||||||
| exeContext, | ||||||||||||||||
| returnType, | ||||||||||||||||
| fieldNodes, | ||||||||||||||||
| info, | ||||||||||||||||
| path, | ||||||||||||||||
| result, | ||||||||||||||||
| ); | ||||||||||||||||
| if (isPromise(completed)) { | ||||||||||||||||
| return completed.then(undefined, error => | ||||||||||||||||
| Promise.reject( | ||||||||||||||||
| locatedError( | ||||||||||||||||
| asErrorInstance(error), | ||||||||||||||||
| fieldNodes, | ||||||||||||||||
| responsePathAsArray(path), | ||||||||||||||||
| ), | ||||||||||||||||
| ), | ||||||||||||||||
| ); | ||||||||||||||||
| } | ||||||||||||||||
| return completed; | ||||||||||||||||
| } catch (error) { | ||||||||||||||||
| throw locatedError( | ||||||||||||||||
| asErrorInstance(error), | ||||||||||||||||
| fieldNodes, | ||||||||||||||||
| responsePathAsArray(path), | ||||||||||||||||
| ); | ||||||||||||||||
| function handleFieldError(rawError, fieldNodes, path, returnType, exeContext) { | ||||||||||||||||
| const error = locatedError( | ||||||||||||||||
| asErrorInstance(rawError), | ||||||||||||||||
| fieldNodes, | ||||||||||||||||
| responsePathAsArray(path), | ||||||||||||||||
| ); | ||||||||||||||||
| | ||||||||||||||||
| // If the field type is non-nullable, then it is resolved without any | ||||||||||||||||
| // protection from errors, however it still properly locates the error. | ||||||||||||||||
| if (isNonNullType(returnType)) { | ||||||||||||||||
| throw error; | ||||||||||||||||
| } | ||||||||||||||||
| | ||||||||||||||||
| // Otherwise, error protection is applied, logging the error and resolving | ||||||||||||||||
| // a null value for this field if one is encountered. | ||||||||||||||||
| exeContext.errors.push(error); | ||||||||||||||||
| return null; | ||||||||||||||||
| } | ||||||||||||||||
| | ||||||||||||||||
| /** | ||||||||||||||||
| | @@ -904,13 +870,6 @@ function completeValue( | |||||||||||||||
| path: ResponsePath, | ||||||||||||||||
| result: mixed, | ||||||||||||||||
| ): MaybePromise<mixed> { | ||||||||||||||||
| // If result is a Promise, apply-lift over completeValue. | ||||||||||||||||
| Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we have a test that shows completing Member Author There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @leebyron Yes, it checked here: graphql-js/src/execution/__tests__/lists-test.js Lines 167 to 173 in 43ff3e3
| ||||||||||||||||
| if (isPromise(result)) { | ||||||||||||||||
| return result.then(resolved => | ||||||||||||||||
| completeValue(exeContext, returnType, fieldNodes, info, path, resolved), | ||||||||||||||||
| ); | ||||||||||||||||
| } | ||||||||||||||||
| | ||||||||||||||||
| // If result is an Error, throw a located error. | ||||||||||||||||
| if (result instanceof Error) { | ||||||||||||||||
| throw result; | ||||||||||||||||
| | ||||||||||||||||
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm concerned moving this out of
completeValuemay break some scenarios we might not have sufficient test coverage over.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@leebyron
completeValueonly called in 3 places:graphql-js/src/execution/execute.js
Lines 907 to 912 in 43ff3e3
I moved this code to
completeValueCatchingErrorso nothing changed here.graphql-js/src/execution/execute.js
Lines 919 to 929 in 43ff3e3
This code is called after promise uplift so nothing changed here.
graphql-js/src/execution/execute.js
Line 849 in 43ff3e3
Also nothing is broken here since this call moved to
completeValueCatchingErrorandcompleteValueCatchingErrornow handles promise uplift by itself.