Our app is using GraphQL to query an SQL database. We have a table of Posts. Users can Like (join) a Post, which creates an item on a Join table. When we query a feed of Posts we have a subquery that grabs the Like. Our like/unlike function checks if this Like object is there in each Post and does the right Mutation. So far this has all worked correctly, but we are having issues with getting it to work optimistically.
The setup looks like this:
type User {
id: ID
username: String
...
}
type Like {
id: ID
userId: ID
postId: ID
}
type Post {
id: ID
message: String
image: Image
likeItem(userID: ID): Like
}
Our Query looks like this
query GetPosts($userId: ID) {
allPosts {
id
message
image {
id
url
}
likeItem({id: $userId}) {
id
}
}
}
This all works. Our question is how do we update something like this optimistically without having to refetch the whole feed? When we look at the cache for “Post:123ABC…” it has multiple “likeItem” keys.
{
...
likeItem({}): null
likeItem({id: "SomeUserId..."}): {__ref: "Like:123ABC..."}
...
}
We could not get cache.writeFragment to update the correct key because the structure is not JSON (thus could not pass it to the data key). Same with cache.writeQuery. So far we have had to use refetchQueries, though it has not been very performant.