I'm trying to update my version of @apollo/client from 3.7.1 to 3.9.3 but am having an issue with any query that uses startPolling and stopPolling.
After upgrade, stopPolling never fires correctly, causing polling to run continuously. Since I have actions that rely on polling having finished, this breaks my app.
Below is an example of one of these queries
export function useMyPolledQuery({
additionalOnCompleted,
skip,
variables,
fetchPolicy = 'cache-and-network',
}: Type) {
const { startPolling, stopPolling, ...queryResult } =
useMyPolledQuery({
skip,
variables,
onCompleted(data) {
additionalOnCompleted?.(data);
const result = data?.myData;
if (result?.status !== myData.Loading) {
return stopPolling();
}
startPolling(1000);
},
onError(error) {
Sentry.captureException(error);
stopPolling();
},
fetchPolicy,
});
return queryResult;
}
I found this issue but nothing suggested here resolves the issue. I've also tried using an explicit work around by using pollInterval and a state setter, something like
export function useMyPolledQuery({
additionalOnCompleted,
skip,
variables,
fetchPolicy = 'cache-and-network',
}: Type) {
const [ stopPolling, setStopPolling ] = useState(false)
const { ...queryResult } =
useMyPolledQuery({
skip,
variables,
onCompleted(data) {
additionalOnCompleted?.(data);
const result = data?.myData;
if (result?.status !== myData.Loading) {
return stopPolling();
}
startPolling(1000);
},
onError(error) {
Sentry.captureException(error);
stopPolling();
},
fetchPolicy,
pollInterval: stopPolling ? 0 : 1000
});
return queryResult;
But this doesn't work either. Does anyone have any suggestions? There's nothing in the documentation to suggest this wouldn't work after package bump.
For anyone else who comes across this, the issue is caused by calling
stopPollingwithin onComplete, without passing a flag ofnotifyOnNetworkStatusChange: true.In previous versions this worked, but now you need to use this option if you want your onComplete to control polling.