···7979method that they call on the `cache` instance a side effect, which may trigger additional cache
8080changes and updates all affected queries as we modify them.
81818282+## Why do we need cache updates?
8383+8484+When we’re designing a GraphQL schema well, we won’t need to write many cache updaters for
8585+Graphcache.
8686+8787+For example, we may have a mutation to update a username on a `User`, which can trivially
8888+update the cache without us writing an updater because it resolves the `User`.
8989+9090+```graphql
9191+query User($id: ID!) {
9292+ user(id: $id) {
9393+ __typename # "User"
9494+ id
9595+ username
9696+ }
9797+}
9898+9999+mutation UpdateUsername($id: ID!, $username: String!) {
100100+ updateUser(id: $id, username: $username) {
101101+ __typename # "User"
102102+ id
103103+ username
104104+ }
105105+}
106106+```
107107+108108+In the above example, `Query.user` returns a `User`, which is then updated by a mutation on
109109+`Mutation.updateUser`. Since the mutation also queries the `User`, the updated username will
110110+automatically be applied by Graphcache. If the mutation field didn’t return a `User`, then this
111111+wouldn’t be possible, and while we can write an updater in Graphcache for it, we should consider
112112+this poor schema design.
113113+114114+An updater instead becomes absolutely necessary when a mutation can’t reasonably return what has
115115+changed or when we can’t manually define a selection set that’d be even able to select all fields
116116+that may update. Some examples may include:
117117+118118+- `Mutation.deleteUser`, since we’ll need to invalidate an entity
119119+- `Mutation.createUser`, since a list may now have to include a new entity
120120+- `Mutation.createBook`, since a given entity, e.g. `User` may have a field `User.books` that now
121121+ needs to be updated.
122122+123123+In short, we may need to write a cache updater for any **relation** (i.e. link) that we can’t query
124124+via our GraphQL mutation directly, since there’ll be changes to our data that Graphcache won’t be
125125+able to see and store.
126126+127127+In a later section on this page, [we’ll learn about the `cache.link` method.](#writing-links-individually)
128128+This method is used to update a field to point at a different entity. In other words, `cache.link`
129129+is used to update a relation from one entity field to one or more other child entities.
130130+This is the most common update we’ll need and it’s preferable to always try to use `cache.link`,
131131+unless we need to update a scalar.
132132+82133## Manually updating entities
8313484135If a mutation field's result isn't returning the full entity it updates then it becomes impossible
···155206156207If we still manage to call any of the cache's methods outside its callbacks in its configuration,
157208we will receive [a "(2) Invalid Cache Call" error](./errors.md#2-invalid-cache-call).
209209+210210+### Updaters on arbitrary types
211211+212212+Cache updates **may** be configured for arbitrary types and not just for `Mutation` or
213213+`Subscription` fields. However, this can potentially be **dangerous** and is an easy trap
214214+to fall into. It is allowed though because it allows for some nice tricks and workarounds.
215215+216216+Given an updater on an arbitrary type, e.g. `Todo.author`, we can chain updates onto this field
217217+whenever it’s written. The updater can then be triggerd by Graphcache during _any_ operation;
218218+mutations, queries, and subscriptions. When this update is triggered, it allows us to add more
219219+arbitrary updates onto this field.
220220+221221+> **Note:** If you’re looking to use this because you’re nesting mutations onto other object types,
222222+> e.g. `Mutation.author.updateName`, please consider changing your schema first before using this.
223223+> Namespacing mutations is not recommended and changes the execution order to be concurrent rather
224224+> than sequential when you use multiple nested mutation fields.
158225159226## Updating lists or links
160227···469536> confused with "real" data in your configuration.
470537471538In the following example we assume that we'd like to implement an optimistic result for a
472472-`favoriteTodo` mutation. The mutation is rather simple and all we have to do is create a function
539539+`favoriteTodo` mutation, like such:
540540+541541+```graphql
542542+mutation FavoriteTodo(id: $id) {
543543+ favoriteTodo(id: $id) {
544544+ id
545545+ favorite
546546+ updatedAt
547547+ }
548548+}
549549+```
550550+551551+The mutation is rather simple and all we have to do is create a function
473552that imitates the result that the API is assumed to send back:
474553475554```js
···491570Once the mutation result comes back from our API this temporary change will be rolled back and
492571discarded.
493572494494-It's important to ensure that our optimistic mutations return all data that the real mutation may
495495-return. If our mutations request a field in their selection sets that our optimistic mutation
496496-doesn't contain then we'll see a warning, since this is a common mistake. To work around not having
497497-enough data we may use methods like `cache.readFragment` and `cache.resolve` to retrieve more data
498498-from our cache.
573573+In the above example optimistic mutation function we also see that `updatedAt` is not present in our
574574+optimistic return value. That’s because we don’t always have to (or can) match our mutations’
575575+selection sets perfectly. Instead, Graphcache will skip over fields and use cached fields for any we
576576+leave out. This can even work on nested entities and fields.
577577+578578+However, leaving out fields can sometimes cause the optimistic update to not apply when we
579579+accidentally cause any query that needs to update accordingly to only be partially cached. In other
580580+words, if our optimistic updates cause a cache miss, we won’t see them being applied.
581581+582582+Sometimes we may need to apply optimistic updates to fields that accept arguments. For instance, our
583583+`favorite` field may have a date cut-off:
584584+585585+```graphql
586586+mutation FavoriteTodo(id: $id) {
587587+ favoriteTodo(id: $id) {
588588+ id
589589+ favorite(since: ONE_MONTH_AGO)
590590+ updatedAt
591591+ }
592592+}
593593+```
499594500500-If we'd like to make sure we don't compute more fields than we need, for instance because one
501501-mutation is run with several different selection sets, then we may pass nested optimistic resolver
502502-functions in our optimistic object, like so:
595595+To solve this, we can return a method on the optimistic result our `optimistic` update function
596596+returns:
503597504598```js
505599const cache = cacheExchange({
···518612```
519613520614The function signature and arguments it receives is identical to the toplevel optimistic function
521521-you define.
615615+you define, and is basically like a nested optimistic function.
522616523617### Variables for Optimistic Updates
524618