I encountered difficulties when designing the schema. My schema is as follows.
type User {
id: String! @id
name: String! @search(by: [term])
followings: [User]
followings_count: Int
followers: [User] @hasInverse(field: followings)
followers_count: Int
...
}
Now there are two users A and B.
1,When A follows B, the A’s followings_count
field will be +1, and B’s followers_count
field will need +1.
This requires a lot of work:
1), Add B to A’s followings, and B’s followers will automatically add A.
2), Query A’s followings_count
3), Update A’s followings_count to followings_count+1
4), Query B’s followers_count
5), Update B’s followers_count to followers_count +1
2,When A unfollows B, Will do something similar to the above.
What’s worse is that GraphQL API does not currently support transaction, once an error occurs, the operation cannot be rolled back.
Is there a better design or method to avoid these redundant work ?