Value object gets overwritten becuase it is normalized
Closed this issue · 3 comments
Entities have ID while value objects does not. If an entity has a value object it still gets normalized with a generated ID.
For example
{
issues {
id
contact {
name
}
}
Here we get a list of issues, where each issue have an ID. Each issue have a contact object which does not have an ID (it is part of the issue).
Now somewhere else we get a single issue and only the phone of the contact:
query($issue: ID) {
issueById($issue) {
id
contact {
phone
}
}
Now the normalized issue will have the same ID and get merged in cache, but the contact will get a different generated ID so the issue will point to a new contact ID which only has the phone attribute. The first query will re-run and find that the contact object is missing name and re-fetch and overwrite issue.contact with an object which only have name. Now the second query will re-run and find that issue.contact is missing name and we have an infinite loop.
There are two ways to solve this:
- Give the value object an ID.
- Don't normalize value objects but instead keep their content inside the owning entity, and merge them on refetch.
Since the value object is logically part of the closest entity, perhaps an alternative way to solve it would be to automatically generate ID for the value object starting from the parent ID. I think today the automatically generated path is the full path in the query which will generate different IDs depending on where in the query the parent entity exists.