Skip to content

Commit 02f2e4f

Browse files
authored
Fix regression causing on{Error,Completed} not to be called (#9226)
1 parent 34ec039 commit 02f2e4f

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## Apollo Client 3.5.7 (unreleased)
2+
3+
### Bug Fixes
4+
5+
- Fix regression that prevented `onError` or `onCompleted` from being called in some cases when using `useQuery`. <br/>
6+
[@mmahalwy](https://github.com/mmahalwy) in [#9226](https://github.com/apollographql/apollo-client/pull/9226)
7+
18
## Apollo Client 3.5.6 (2021-12-07)
29

310
### Bug Fixes (by [@brainkim](https://github.com/brainkim) in [#9144](https://github.com/apollographql/apollo-client/pull/9144))

src/react/hooks/__tests__/useQuery.test.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,17 +1545,20 @@ describe('useQuery Hook', () => {
15451545
await waitForNextUpdate();
15461546
expect(result.current.loading).toBe(false);
15471547
expect(result.current.data).toEqual(data1);
1548+
expect(onCompleted).toHaveBeenLastCalledWith(data1);
15481549

15491550
rerender({ variables: { first: 2 } });
15501551
expect(result.current.loading).toBe(true);
15511552

15521553
await waitForNextUpdate();
15531554
expect(result.current.loading).toBe(false);
15541555
expect(result.current.data).toEqual(data2);
1556+
expect(onCompleted).toHaveBeenLastCalledWith(data2);
15551557

15561558
rerender({ variables: { first: 1 } });
15571559
expect(result.current.loading).toBe(false);
15581560
expect(result.current.data).toEqual(data1);
1561+
expect(onCompleted).toHaveBeenLastCalledWith(data1);
15591562

15601563
expect(onCompleted).toHaveBeenCalledTimes(3);
15611564
});

src/react/hooks/useQuery.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,10 @@ export function useQuery<
134134

135135
setResult(ref.current.result = nextResult);
136136
if (!nextResult.loading && options) {
137-
if (!result.loading) {
138-
if (result.error) {
139-
options.onError?.(result.error);
140-
} else if (result.data) {
141-
options.onCompleted?.(result.data);
142-
}
137+
if (nextResult.error) {
138+
options.onError?.(nextResult.error);
139+
} else if (nextResult.data) {
140+
options.onCompleted?.(nextResult.data);
143141
}
144142
}
145143
}

0 commit comments

Comments
 (0)