-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[fix] whenResultIsFinished works on Promise of Array of Promises (#7843)
Paired with @bbeesley. Updates whenResultIsFinished to call callback also on Promise of Array of Promises. Fixes (partially) #5372 and #4667. The test case we're fixing is `'passes result of Promise of Array of Promises to the callback'`, the other tests test existing behaviour. The existing bug prevented us from dynamically setting cache hints in the reference resolver depending on the result of an asynchronous operation. We discovered the bug within an integration test. If you @glasser have an idea how to add an integration test and would like to add it, that would be amazing! --------- Co-authored-by: David Glasser <glasser@apollographql.com>
- Loading branch information
1 parent
5704146
commit 72f568e
Showing
3 changed files
with
45 additions
and
2 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 |
---|---|---|
@@ -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