Easily mutate resource in multiple keys #2321
Unanswered
nicholaschiang
asked this question in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Bug report
Description / Observed Behavior
I have multiple endpoints and possible queries for message data fetched via SWR:
/api/messages
- All messages./api/messages?archive=true
- Archived messages./api/messages?archive=true&[email protected]
- Archived messages from a specific writerThis becomes even more complicated when completely different endpoints return the same resource (highlights):
/api/highlights
- All highlights./api/messages/[id]/highlights
- This message's highlights./api/highlights?deleted=true
- Deleted highlights.Right now, if I update a single highlight, I have to manually mutate each SWR key, see if that highlight existed there, and update it bsaed off existing data like so:
Expected Behavior
How did you expect SWR to behave here?
SWR should expose an API to keep track of different resource types and their associated keys. For example, SWR could store a map of resource types and their arrays of keys like so:
And then SWR would be able to expose an API to easily update a single resource's data across every single possible key that it could've appeared in! I think this is a pretty common use-case: to have an API endpoint that returns a list of data and then another page that updates only a single piece of data in that list.
Repro Steps / Code Example
Here's a custom hook I've been working on that helps with this (it also helps keep track of those mutations so that I don't revalidate when there's mutated list data UNTIL after it's updated server-side):
Beta Was this translation helpful? Give feedback.
All reactions