T
TanStack2mo ago
eastern-cyan

[solved] useQuery ignores `retry: false` when `refetchInterval` is set

Title pretty much says it all. Here is my query:
const drivesQuery = useQuery({
queryKey: ["drives"],
retry: false,
queryFn: () => window.electron.GetVolume(),
// refetchInterval: 5,
});
const drivesQuery = useQuery({
queryKey: ["drives"],
retry: false,
queryFn: () => window.electron.GetVolume(),
// refetchInterval: 5,
});
As soon as I uncomment refetchInterval: 5, the query will indefinitely retry (as fast as it can) instead of simply stopping after the first error, which currently always happens (expected). What am I doing wrong? I do want the query to refetch, but only if there were no errors.
5 Replies
eastern-cyan
eastern-cyanOP2mo ago
Query client config:
const queryClient = new QueryClient({
defaultOptions: {
queries: {
refetchOnWindowFocus: false,
refetchOnReconnect: false,
retry: false,
},
},
});
const queryClient = new QueryClient({
defaultOptions: {
queries: {
refetchOnWindowFocus: false,
refetchOnReconnect: false,
retry: false,
},
},
});
like-gold
like-gold2mo ago
retry and refetchInterval are not the same thing retry defines how often query should retry running the queryFn before it goes into error state. interval schedules a new run of the queryFn once it completes the previous run (including all retries) or otherwise gets data set so yes, with zero retries and an interval of 5ms, you get a run every 5ms with the default of 3 retries and exponential backoff and an interval of 5, you would get: - run queryFn with 3 retries and pauses in between (that takes about 20-30s) - wait 5ms - run queryFn with 3 retries again etc so, not sure what you're trying to achieve but with that config, that's the expected behaviour
eastern-cyan
eastern-cyanOP2mo ago
oh, i think i kind of understand. what i'm trying to achieve: - refetch every 5 seconds if the previous query was successful or there is no previous query (first query) - if a query was unsuccessful, immediately go into error state and do not retry
like-gold
like-gold2mo ago
so retry: false and implement the refetchInterval function to only set an interval if the query is in success state
eastern-cyan
eastern-cyanOP2mo ago
got it. so i had 2 errors: thinking the value for refetchInterval was seconds where it's actually miliseconds, and also that specifying a refetchInterval ignores retry by default and i need to implement that myself. thanks a lot! got it to work just like i need 👍
const drivesQuery = useQuery({
queryKey: ["drives"],
retry: false,
queryFn: () => window.electron.GetVolume(),
refetchInterval: (q) => (q.state.status === "error" ? false : 2000),
});
const drivesQuery = useQuery({
queryKey: ["drives"],
retry: false,
queryFn: () => window.electron.GetVolume(),
refetchInterval: (q) => (q.state.status === "error" ? false : 2000),
});

Did you find this page helpful?