Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass query response as an arg to key function #159

Closed
Devin-CS opened this issue Jan 13, 2025 · 1 comment
Closed

Pass query response as an arg to key function #159

Devin-CS opened this issue Jan 13, 2025 · 1 comment

Comments

@Devin-CS
Copy link

One of my favorite features of RTK Query (in the React ecosystem) is the ability to dynamically generate the key, based on the query results, right before it gets cached. For example, when fetching a list of Todos:

// [{ type: 'Todo' }, { type: 'Todo', id: 1 }, { type: 'Todo', id: 2 }, { type: 'Todo', id: 3 }, ...]
key: (response) => [{ type: 'Todo' }, ...response.data.map(todo => ({ type: 'Todo', id: todo.id }))]

This way, when mutating a specific Todo (such as id: 5), I can invalidate any and all cached queries that contain { type: 'Todo', id: 5 }, while not, unnecessarily, invalidating any Todo caches that don't have that specific Todo.

@posva
Copy link
Owner

posva commented Jan 14, 2025

the ability to dynamically generate the key, based on the query results, right before it gets cached.

I don't think this is a good idea because when querying, you already have an id to use, not having the key ready before fetching wouldn't allow deduplication. In Pinia Colada, the entries with the state require the key to be computed before actually fetching the data, so it won't be possible.

This way, when mutating a specific Todo (such as id: 5), I can invalidate any and all cached queries that contain

You can already do that in mutations with the hooks (not yet documented). They get passed the variables passed to the mutation handler, which allows you to precisely invalidate queries.

@posva posva closed this as not planned Won't fix, can't repro, duplicate, stale Jan 14, 2025
@github-project-automation github-project-automation bot moved this from 🆕 Triaging to Done in Pinia Colada Roadmap Jan 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

No branches or pull requests

2 participants