Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support InMemoryCache field policy drop functions, to allow cleanup when fields are removed from the cache #8078

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
- `InMemoryCache` now _guarantees_ that any two result objects returned by the cache (from `readQuery`, `readFragment`, etc.) will be referentially equal (`===`) if they are deeply equal. Previously, `===` equality was often achievable for results for the same query, on a best-effort basis. Now, equivalent result objects will be automatically shared among the result trees of completely different queries. This guarantee is important for taking full advantage of optimistic updates that correctly guess the final data, and for "pure" UI components that can skip re-rendering when their input data are unchanged. <br/>
[@benjamn](https://github.com/benjamn) in [#7439](https://github.com/apollographql/apollo-client/pull/7439)

- In addition to `read` and `merge` functions, `InMemoryCache` field policies may now configure a `drop` function, which will be called just before the field in question is removed from the cache, facilitating any final field cleanup that may be needed by `read` and `merge`. <br/>
[@benjamn](https://github.com/benjamn) in [#8078](https://github.com/apollographql/apollo-client/pull/8078)

- `InMemoryCache` supports a new method called `batch`, which is similar to `performTransaction` but takes named options rather than positional parameters. One of these named options is an `onDirty(watch, diff)` callback, which can be used to determine which watched queries were invalidated by the `batch` operation. <br/>
[@benjamn](https://github.com/benjamn) in [#7819](https://github.com/apollographql/apollo-client/pull/7819)

Expand Down
80 changes: 80 additions & 0 deletions src/cache/inmemory/__tests__/__snapshots__/policies.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1211,6 +1211,86 @@ Object {
}
`;

exports[`type policies field policies custom field policy drop functions are called if merge function returns undefined 1`] = `
Object {
"ROOT_QUERY": Object {
"__typename": "Query",
"todoList": Object {
"__ref": "ToDoList:{}",
},
},
"Task:{\\"taskID\\":1}": Object {
"__typename": "Task",
"taskID": 1,
"text": "task #1",
},
"Task:{\\"taskID\\":2}": Object {
"__typename": "Task",
"taskID": 2,
"text": "task #2",
},
"ToDoList:{}": Object {
"__typename": "ToDoList",
"tasks": Array [
Object {
"__ref": "Task:{\\"taskID\\":1}",
},
Object {
"__ref": "Task:{\\"taskID\\":2}",
},
],
},
}
`;

exports[`type policies field policies custom field policy drop functions are called if merge function returns undefined 2`] = `
Object {
"ROOT_QUERY": Object {
"__typename": "Query",
"todoList": Object {
"__ref": "ToDoList:{}",
},
},
"Task:{\\"taskID\\":1}": Object {
"__typename": "Task",
"taskID": 1,
"text": "task #1",
},
"Task:{\\"taskID\\":2}": Object {
"__typename": "Task",
"taskID": 2,
"text": "task #2",
},
"Task:{\\"taskID\\":3}": Object {
"__typename": "Task",
"taskID": 3,
"text": "task #3",
},
"Task:{\\"taskID\\":4}": Object {
"__typename": "Task",
"taskID": 4,
"text": "task #4",
},
"ToDoList:{}": Object {
"__typename": "ToDoList",
"tasks": Array [
Object {
"__ref": "Task:{\\"taskID\\":1}",
},
Object {
"__ref": "Task:{\\"taskID\\":2}",
},
Object {
"__ref": "Task:{\\"taskID\\":3}",
},
Object {
"__ref": "Task:{\\"taskID\\":4}",
},
],
},
}
`;

exports[`type policies field policies read, merge, and modify functions can access options.storage 1`] = `
Object {
"ROOT_QUERY": Object {
Expand Down
Loading