Auto with pollingInterval variable

The useQuery hook returns startPolling and stopPolling functions that can be used to control when to start polling.

Challenge

  1. useQuery will immediately send a first query, before you start polling
  2. When specifying skip in the options, startPolling stops working in current version 😂
  3. useLazyQuery does not work well with polling either for current version

Manual

It’s not too hard to implement manual polling leveraging useLazyQuery feature.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const [getXXX] =
useLazyQuery <XXXData>
(XXXQuery,
{
variables: {
XXX variables
},
fetchPolicy: 'network-only',
})

const startPolling = () => {
let attemptCounter = 0

const pollXXX = async () => {
attemptCounter = attemptCounter + 1
try {
const { data } = await geXXX()
if (gotFinishedXXX) {
onComplete(data.XXX)
return
}
await wait(getPollingTimeout(attemptCounter))
pollTransaction()
} catch {
// Handle error
return
}
}
pollXXX()
}

return startPolling