-
Notifications
You must be signed in to change notification settings - Fork 2k
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
[fix] whenResultIsFinished works on Promise of Array of Promises #7843
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@apollo/server": patch | ||
--- | ||
|
||
Improves timing of the `willResolveField` end hook on fields which return Promises resolving to Arrays. This makes the use of the `setCacheHint` method more reliable. |
37 changes: 37 additions & 0 deletions
37
packages/server/src/__tests__/utils/schemaInstrumentation.test.ts
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { describe, expect, it, jest } from '@jest/globals'; | ||
import { whenResultIsFinished } from '../../utils/schemaInstrumentation'; | ||
|
||
describe('whenResultIsFinished', () => { | ||
it('passes result of Promise to the callback', async () => { | ||
const expected = 1; | ||
const result = Promise.resolve(expected); | ||
const callback = jest.fn(); | ||
whenResultIsFinished(result, callback); | ||
await new Promise((r) => setImmediate(r)); | ||
expect(callback).toBeCalledWith(null, expected); | ||
}); | ||
it('passes result of Array of Promises to the callback', async () => { | ||
const expected = 1; | ||
const result = [Promise.resolve(expected)]; | ||
const callback = jest.fn(); | ||
whenResultIsFinished(result, callback); | ||
await new Promise((r) => setImmediate(r)); | ||
expect(callback).toBeCalledWith(null, [expected]); | ||
}); | ||
it('passes result which is not asynchronous directly to the callback', async () => { | ||
const expected = 1; | ||
const result = expected; | ||
const callback = jest.fn(); | ||
whenResultIsFinished(result, callback); | ||
await new Promise((r) => setImmediate(r)); | ||
expect(callback).toBeCalledWith(null, expected); | ||
}); | ||
it('passes result of Promise of Array of Promises to the callback', async () => { | ||
const expected = 1; | ||
const result = Promise.resolve([Promise.resolve(expected)]); | ||
const callback = jest.fn(); | ||
whenResultIsFinished(result, callback); | ||
await new Promise((r) => setImmediate(r)); | ||
expect(callback).toBeCalledWith(null, [expected]); | ||
}); | ||
}); |
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 |
---|---|---|
|
@@ -106,14 +106,15 @@ function isPromise(x: any): boolean { | |
|
||
// Given result (which may be a Promise or an array some of whose elements are | ||
// promises) Promises, set up 'callback' to be invoked when result is fully | ||
// resolved. | ||
// resolved. (Unfortunately, this does not perfectly handle every possible | ||
// return value shape, such as arrays of arrays of Promises.) | ||
export function whenResultIsFinished( | ||
result: any, | ||
callback: (err: Error | null, result?: any) => void, | ||
) { | ||
if (isPromise(result)) { | ||
result.then( | ||
(r: any) => callback(null, r), | ||
(r: any) => whenResultIsFinished(r, callback), | ||
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. This specifically improves the "Promise of Array of ..." case. |
||
(err: Error) => callback(err), | ||
); | ||
} else if (Array.isArray(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 not super positive this is guaranteed (the callback will be called in parallel with the await finishing?) but if the test isn't flaky that's OK for me.