Company
Date Published
Author
Khalil Stemmler
Word count
3666
Language
English
Hacker News points
None

Summary

The Apollo Client normalization algorithm works by splitting the response data into individual objects, assigning a unique identifier to each object, and storing them in a flattened JavaScript object. The cache can automatically update single existing entities in the cache after mutations if the mutation returns the new value in the response. However, for operations that do not return the entire set of changed items or updates to local state variables, we need to write an update function to tell the cache exactly how to update. Similarly, for additions and deletions, we also need to update the cache manually by filtering out or removing items from the existing query. The cache does not make assumptions about how you would like your collections/arrays of items to change after a mutation, so in these cases, we need to decide what the appropriate thing to do is and implement it in the update function of a mutation.