This stack overflow post talks about the when to use the mutation hook vs the useQuery hook. The gist of it was:
- If you're reading data:
useQuery - If you're creating/updating/deleting data:
useMutation
Is there a good approach/solution for workflows that involve creating a task, polling its status and fetching its results (in my example these are 3 separate endpoints)?
The useQuery hook seems to be a nightmare for this because it often involves:
- a mutationFn to start the process
- separate useQuery hooks for polling and fetching results
- enabling/disabling each hook based on some state variables
- extra code and hooks outside of the function to update the application with the data returned
- The builtin
refetchandretryare a plus here though
The mutation hook isn't as bad:
- I shove all the logic into the one mutationFn
- I have to write the polling logic and the retry logic manually
Does others that use this library have an approach to this?
Here's the gist of what I do today
mutationFn: async () => {
const data = await startTask();
let status = await getStatus(data);
// I'll need to manually add some retry logic here
while (status === "processing") {
await sleep(1000);
status = await getStatus(data);
}
const results = await getResults(data);
return results;
}