blitz-js / blitz-js/blitz

Automatic Cache Invalidation

Open
#586 16 comments 2 reactions 0 assignees View on GitHub
kind/feature-change status/ready-to-define-implementation
Dominant language
TypeScript
Stars
14.1k
Forks
803
PR merge metrics
No merged PRs in 30d

Description

### What do you want and why?

Currently you need to manually call `refetch()` or [`mutate()`](#582) 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](#583), but that still requires you to think about it.

**I'm super keen to hear anyone's ideas on this!**

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.