apollo-feature-requests
apollo-feature-requests copied to clipboard
Idea how to improve Caching using relation modeling
So today in order to update cache correctly, you should use the cache api after an object is added, deleted, and also if update causes relational changes.
So my idea is to have a way to define the relationships much like ORMs and that will make the cache much smarter. for example:
say you have this:
Post {
id: number,
comments: Comments[]
}
Comment {
id: number;
postId: number;
post: Post;
}
If today we would like to edit postId for the comment(just for example sake) we would have to
- update 2 Post elements, one of them will be the old attached Post, and remove the comment attached from it, the other will be the new attached Post, adding the comment to it.
- update the comment itself, telling the post field to reference the new postId.. or alternatively have the mutation return something like this:
myCommentMutation(...) {
postId // will have the new postId
post { // Will force apollo client to reference the new post we now point to (via postId)
id
}
}
So I believe that if apollo client will have a way to define, much like modern ORM's, a way to declare relationships between fields, one to many, many to one.. The user experience for updating the cache will be much simpler.. 99% of the cases will be spared from the user.
In my case specifically we could write the mutation something like this:
myCommentMutation(...) {
postId // will have the new postId
// This is no longer needed - START
post { // Will force apollo client to reference the new post we now point to (via postId)
id
}
// This is no longer needed - END
}
and also, we will not have to update the 2 Post elements, since apollo would know in advance to remove the comment from the initial Post.comments field, and add to the new Post.comments field.
Looking forward hearing your thoughts on the matter as well