Can I use refetchQueries with some delay. Can I set a delay or can I call refetchQueries in a function? Thanks!
Asked
Active
Viewed 2,474 times
4
-
are you looking for something like this https://stackoverflow.com/questions/50965347/how-to-execute-an-async-fetch-request-and-then-retry-last-failed-request/513210 ? – Harsh Makadia Aug 13 '19 at 06:59
-
2@HarshMakadia that answer seems to be about requesting failed queries, sometimes we may wish to delay refetch if there are known latencies on the backend – Damian Green Aug 22 '19 at 16:06
-
@DamianGreen yeah, you are right – Mehmet Ali Peker Aug 28 '19 at 12:14
1 Answers
1
I was unable to find an elegant way to delay refetchQueries
. Instead, I just modify the cache directly as mentioned farther down this section of the documentation. https://www.apollographql.com/docs/react/caching/advanced-topics/#updating-after-a-mutation
Here's a snippet from my solution. The reason I loop through sortPermutations
is that the query may have cached data for different sets of variables
. This solution loops through all of them.
const [createUser] = useMutation<AddUser, AddUserVariables>(ADD_USER, {
update: (cache, { data }) => {
if (!data?.addUser) return
const cachedUsers = cache.readQuery<GetUsers>({
query: GET_USERS,
variables: sortedColumn,
})
sortPermutations.forEach(({ sortBy, sortOrder }) => {
const users = [data.addUser, ...(cachedUsers?.users ?? [])].sort(
sortUsers(sortBy, sortOrder),
)
cache.writeQuery<GetUsers, GetUsersVariables>({
data: { users },
query: GET_USERS,
variables: { sortBy, sortOrder },
})
})
},
})

hboylan
- 357
- 3
- 12