Skip to content
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 4 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/long-crabs-reply.md
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 packages/server/src/__tests__/utils/schemaInstrumentation.test.ts
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]);
Copy link
Member

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.

});
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]);
});
});
5 changes: 3 additions & 2 deletions packages/server/src/utils/schemaInstrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Copy link
Member

Choose a reason for hiding this comment

The 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)) {
Expand Down