Send revalidated data alongside mutation in tRPC
Hi. How would I go about sending new data from my server when there is a mutation made? I have a query that fetches posts and it seems useless to send a mutation and once that mutation succeeds, send a new request for all new posts. Is there realistically an option to re-send the posts from the server alongside the mutation with some tRPC magic, so that it gets new state quicker? Or how should I architect this?
3 Replies
like realistically a user shouldn't have more than say ~1s delay at most from mutating to getting the new data back but i mean if i could reduce that it would be nice
you have two options roughly speaking
1. invalidate after the mutation completes, possibly with optimistic ui prior to that
2. manage cache manually
whatever you return from a mutation is available in its onSuccess (so you could manually update the cache once your mutation is successful)
but i generally find that to be the wrong approach as it goes into manual cache management, which is a place you probably don't want to be
better: optimistically update the UI in
onMutate
, invalidate the query in onSettled
that way you DO completely refetch the query, but:
1. you're showing updated UI as soon as the user clicks a thing
2. if your manual cache invalidation ends up being incorrect, it gets fixed once the onSettled
callback runsyeah manual cache management isnt what i want tbh
thanks 👍