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

QueryReference: check for resolve/reject #10999

Merged
merged 2 commits into from
Jun 23, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/little-schools-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@apollo/client': patch
---

Fix a bug in `QueryReference` where `this.resolve` or `this.reject` might be executed even if `undefined`.
12 changes: 8 additions & 4 deletions src/react/cache/QueryReference.ts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind writing a test for this? I'll be refactoring this a bit and want to make sure I don't introduce a regression. According to the discord message, it looks like subsequent fetches after a cache hit will cause this. Would love to have a test that shows this as fixed. Thanks!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can try. I have to admit I didn't have enough context on how to potentially trigger this.

Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ export class QueryReference<TData = unknown> {
private initialized = false;
private refetching = false;

private resolve: (result: ApolloQueryResult<TData>) => void;
private reject: (error: unknown) => void;
private resolve: ((result: ApolloQueryResult<TData>) => void) | undefined;
private reject: ((error: unknown) => void) | undefined;

constructor(
observable: ObservableQuery<TData>,
Expand Down Expand Up @@ -157,7 +157,9 @@ export class QueryReference<TData = unknown> {
this.initialized = true;
this.refetching = false;
this.result = result;
this.resolve(result);
if (this.resolve) {
this.resolve(result);
}
return;
}

Expand All @@ -182,7 +184,9 @@ export class QueryReference<TData = unknown> {
if (!this.initialized || this.refetching) {
this.initialized = true;
this.refetching = false;
this.reject(error);
if (this.reject) {
this.reject(error);
}
return;
}

Expand Down
49 changes: 49 additions & 0 deletions src/react/hooks/__tests__/useSuspenseQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4408,6 +4408,55 @@ describe('useSuspenseQuery', () => {
]);
});

it('suspends when refetching after returning cached data for the initial fetch', async () => {
const { query, mocks } = useSimpleQueryCase();

const cache = new InMemoryCache();

cache.writeQuery({
query,
data: { greeting: 'hello from cache' },
});

const { result, renders } = renderSuspenseHook(
() => useSuspenseQuery(query),
{ cache, mocks }
);

expect(result.current).toMatchObject({
data: { greeting: 'hello from cache' },
networkStatus: NetworkStatus.ready,
error: undefined,
});

act(() => {
result.current.refetch();
});

await waitFor(() => {
expect(result.current).toMatchObject({
data: { greeting: 'Hello' },
networkStatus: NetworkStatus.ready,
error: undefined,
});
});

expect(renders.count).toBe(3);
expect(renders.suspenseCount).toBe(1);
expect(renders.frames).toMatchObject([
{
data: { greeting: 'hello from cache' },
networkStatus: NetworkStatus.ready,
error: undefined,
},
{
data: { greeting: 'Hello' },
networkStatus: NetworkStatus.ready,
error: undefined,
},
]);
});

it('properly uses `updateQuery` when calling `fetchMore`', async () => {
const { data, query, link } = usePaginatedCase();

Expand Down