Skip to content

Commit

Permalink
Merge pull request #6588 from apollographql/issue-6301
Browse files Browse the repository at this point in the history
Tolerate !== onCompleted and onError callbacks passed as options to useQuery.
  • Loading branch information
benjamn authored Jul 20, 2020
2 parents 7993a77 + 462e6a5 commit eb33af9
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
- Rework interdependencies between `@apollo/client/*` entry points, so that CommonJS and ESM modules are supported equally well, without any duplication of shared code. <br/>
[@benjamn](https://github.com/benjamn) in [#6058](https://github.com/apollographql/apollo-client/pull/6058)

- Tolerate `!==` callback functions (like `onCompleted` and `onError`) in `useQuery` options, since those functions are almost always freshly evaluated each time `useQuery` is called. <br/>
[@hwillson](https://github.com/hwillson) and [@benjamn](https://github.com/benjamn) in [#6588](https://github.com/apollographql/apollo-client/pull/6588)

## Apollo Client 3.0.2

## Bug Fixes
Expand Down
17 changes: 14 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"dependencies": {
"@types/zen-observable": "^0.8.0",
"@wry/context": "^0.5.2",
"@wry/equality": "^0.1.9",
"@wry/equality": "^0.2.0",
"fast-json-stable-stringify": "^2.0.0",
"graphql-tag": "^2.10.4",
"hoist-non-react-statics": "^3.3.2",
Expand Down
37 changes: 37 additions & 0 deletions src/react/hooks/__tests__/useQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1814,6 +1814,43 @@ describe('useQuery Hook', () => {

return wait().then(resolve, reject);
});

itAsync(
'should not make extra network requests when `onCompleted` is ' +
'defined with a `network-only` fetch policy',
(resolve, reject) => {
let renderCount = 0;
function Component() {
const { loading, data } = useQuery(CAR_QUERY, {
fetchPolicy: 'network-only',
onCompleted: () => undefined
});
switch (++renderCount) {
case 1:
expect(loading).toBeTruthy();
break;
case 2:
expect(loading).toBeFalsy();
expect(data).toEqual(CAR_RESULT_DATA);
break;
case 3:
fail('Too many renders');
default:
}
return null;
}

render(
<MockedProvider mocks={CAR_MOCKS}>
<Component />
</MockedProvider>
);

return wait(() => {
expect(renderCount).toBe(2);
}).then(resolve, reject);
}
);
});

describe('Optimistic data', () => {
Expand Down

0 comments on commit eb33af9

Please sign in to comment.