blitz: Automatic Cache Invalidation
What do you want and why?
Currently you need to manually call refetch()
or mutate()
on a useQuery
result after doing a mutation.
We should try to come up with a way to automatically invalidate queries so that most users don’t have to think about it all.
I haven’t spent a lot of time thinking about this, but here’s a couple options.
Based on file structure
We could automatically invalidate queries at the same folder level of the mutation.
So with this file structure,
app/products/queries/getProduct.ts
app/products/queries/getProducts.ts
app/products/mutations/createProduct.ts
app/products/mutations/updateProduct.ts
app/products/mutations/deleteProduct.ts
Calling createProduct
, updateProduct
, or deleteProduct
would invalidate the cache for getProduct
and getProducts
.
But the problem with this is that app/queries/getDisplayProducts.ts
would not be invalidated even thought it is also affected. But maybe this is still worth it if it solves the 80% use case.
Via Some Type of Introspection Magic
Maybe it’s possible for us to compute the entire data graph at build time using a combination of the Prisma DMMF and Typescript? If possible, this would be amazing because we could accurately invalidate all queries anywhere in the app.
I think this would only work with Prisma, so we should still have some other fallback method for Blitz apps without Prisma.
On Route Change
Something else that’s a possibility is busting the cache on route change, but that still requires you to think about it.
I’m super keen to hear anyone’s ideas on this!
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 2
- Comments: 16 (8 by maintainers)
Highly anticipated feature
What about a list of queries kind of like dependencies in the useEffect hook?
It’s not! We just haven’t had the time to work on it yet.
👋 I’m far from an expert but I’ve been reading through the docs and wondering how to do this myself. What about passing in a query to the
useMutation
hook and that query gets invalidated once a mutation has succeeded?@8balloon thank you, I love it!
I’ve caught wind of some possible caching changes in react-query that could be very useful here, so I think we should sit on this until we get more details on any react-query changes.
Optionally declare the list as a named export in the mutation file to make it somewhat more automatic. (or even a list of linked models to invalidate related queries from. That way we aren’t over eager?)
I think the explicitness of declaring them as dependencies of the hook directly lets other developers know what queries/models are dependent without having to jump to a different context, but I also imagine writing it out a thousand times would be time consuming/frustrating