how to update fragment like graphql cache does ? #1731
Replies: 4 comments 1 reply
-
You can invalidate the cache of /users/posts calling It means if you are on the lists of posts, go to your profile, edit a post, go back to list of posts the list will initially be stale then SWR will fetch the API again to revalidate it against the API, which will get the updated post back and update the UI. |
Beta Was this translation helpful? Give feedback.
-
can't we add some functionality to represent data as collection / entity / model which will handle cache invalidation process where we don't have to do it manually, if we change any entity, that functionality make sure to update all entity, For Example class User extends Entity {} const collection = new Collection() const users = useSWR("/users") User.find(3).cache().invalidate() |
Beta Was this translation helpful? Give feedback.
-
That will do too many suppositions about the data, SWR right now it’s completely agnostic of what you fetch and the shape of it. Maybe you are fetching something which doesn’t have ids and can’t be represented as an entity (e.g. a list of strings), or maybe you are fetching a jsonapi.org compatible API, or a GraphQL endpoint or gRCP, SWR works for all of them right now. However I’m not a maintainer, let see what the ZEIT team thinks. |
Beta Was this translation helpful? Give feedback.
-
Changing this to discussion — it’s not something planned for the near future, as SWR’s current goal is to remain minimal and backend agnostic. However if we can figure out a good way to define the relationship of resources, we can definitely revisit this topic. Thanks for your feedback and understanding! |
Beta Was this translation helpful? Give feedback.
-
So, Imagine if you call api and cache the data, now if you want to update part of data how would you do that ?
for example:- It's React Native project, there are 2 endpoints,
now imagine we call first endpoint, get data and cache it, and then I go to my profile, and see all my post, now I update one of my post title,
Question is how am i gonna update all cached data, /users/posts also has my posts and there may be some other endpoints where the same post exists which is not updated,
In GraphQL we can update fragment, I am not sure if it is even possible to do it with this ?
Beta Was this translation helpful? Give feedback.
All reactions