Why aggressive normalization?
stalniy opened this issue · 3 comments
After playing with the library I see that you normalize all objects. What is the purpose of normalizing everything and not only objects which has __typename
and id
?
This is a good question and partly related to #27. We could normalize only object's that have ID. However then the structure will not be 100% flat since some normalized objects may have fields with deep structure (becuase some of their child objects have no ID). Not having a 100% flat structure brings some problems:
- When merging, we probably need to do a deep merge instead of just shallow.
- When denormalizing, we need to determine if we should treat a field as normalized or not (today everyhing is normalized so there is no need to determine it).
So the reason for normalizing everything is that the logic becomes easier, however it brings problems like in #27. I read an issue in the apollo repo about this and it seems they also normalize every object, probably for the same reasons (I can't seem to find that issue right now though).
yes, apollo also normalize the whole graph (but they also save some additional meta information). Now I understand the reasoning behind this decision, thanks!
It seems like in Apollo Client 3 they are moving towards not normalizing objects without ID:
a) The InMemoryCache no longer generates local IDs
Previous versions of the InMemoryCache would generate identifiers for entities that lack one in order to enable normalization. In Apollo Client 3, theInMemoryCache uses a new, reference-ID-based approach that only normalizes entities that have an ID. Entities without an ID are instead stored within their parent object in the cache. Initial tests with production data sets show a dramatic reduction in cache size, and an increase in overall cache performance.
Might be interesting to look into this more.