In my solution, I am wrapping React Query's useQuery hook with my own to have a specific retry logic:
export function useMyQuery(key, queryFn, options = {}) {
const queryClient = useQueryClient();
const query = useReactQuery(key, queryFn, {
...options,
retry: async(count, error) => await retry(count, error, queryClient),
});
return query;
}
The retry function itself is as follows:
export async function retry(count, error, queryClient) {
if (error?.statusCode === 401) {
// caught 401, trying to refresh token;
if (await refreshToken(queryClient)) {
queryClient.refetchQueries('fetchSession');
return true;
}
}
// Retry 3 times
return count < 3;
}
and, finally my fetchSession query looks like this:
const { data } = useQuery('fetchSession', () => {
console.log('fetching new session...');
return Auth.currentSession();
})
My Goal is to trigger refetch of the "fetchSession" query after I have successfully refreshed the token in the code above, however, the query is never refetched i.e. the query body is never run after the token is refreshed. Besides using refetchQueries method on queryClient, I have also tried invalidateQueries to no effect.