T
TanStack3y ago
other-emerald

How to update query data without resetting refetchInterval timer?

I have a query that should be re-fetched every X seconds. The solution is to use refetchInterval. But I noticed that calling setQueryData resets the refetchInterval timer. In my case, I have a websocket connection that updates part of a query data every 100 milliseconds (with setQueryData). If the refetchIntervalis larger than 100ms, the queryFn will never re-run. But I want the queryFn to re-run because the websocket only updates part of the query data and I want to keep the rest of it in sync with the server using refetchInterval. Our API socks and this is probably an edge case, but I have no choice but dealing with it. Is there a solution?
2 Replies
deep-jade
deep-jade3y ago
Why do you need an interval if youre getting data every 100ms anyways? You can pass dataUpdatedAt to setQueryData
other-emerald
other-emeraldOP3y ago
I don't get all the data from the websocket. For example, this is what queryFn returns:
{
queryData: 1,
websocketData: 1
}
{
queryData: 1,
websocketData: 1
}
The websocket only updates websocketData:
{
websocketData: 2
}
{
websocketData: 2
}
I need to re-fetch the query based on refetchInterval to sync queryData with the server. That's why I need both. I also tried using updatedAt but it didn't work. This is how I used it:
const query = useQuery(/* ... */)

queryClient.setQueryData(queryKey, newData, {
updatedAt: query.dataUpdatedAt,
});
const query = useQuery(/* ... */)

queryClient.setQueryData(queryKey, newData, {
updatedAt: query.dataUpdatedAt,
});

Did you find this page helpful?