-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
benjamn
wants to merge
8
commits into
main
Choose a base branch
from
InMemoryCache-field-policy-finalize-function
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
640d504
Support InMemoryCache field policy `drop` function.
benjamn e4ee0be
Address a remaining TODO.
benjamn 621986d
Avoid recreating function object in inner loop of assignPaths.
benjamn 742a362
Use helper function more consistently in getStorage.
benjamn 561ecca
Compare existing data to merged data when scanning for fields to drop.
benjamn d246ad6
Mention PR #8078 in CHANGELOG.md.
benjamn 8545da8
Make group.assignPaths processing lazier.
benjamn bdadc99
Do less work when no drop functions have been configured.
benjamn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1705,6 +1705,308 @@ describe("type policies", function () { | |
expect(cache.extract()).toMatchSnapshot(); | ||
}); | ||
|
||
describe("custom field policy drop functions", function () { | ||
const makeCache = (resolve: () => void) => new InMemoryCache({ | ||
typePolicies: { | ||
Parent: { | ||
keyFields: false, | ||
fields: { | ||
deleteMe: { | ||
read(existing, { storage }) { | ||
expect(existing).toBe("merged value"); | ||
expect(storage.cached).toBe(existing); | ||
return "read value"; | ||
}, | ||
merge(existing, incoming, { storage }) { | ||
expect(existing).toBeUndefined(); | ||
expect(incoming).toBe("initial value"); | ||
return storage.cached = "merged value"; | ||
}, | ||
drop(existing, { storage }) { | ||
expect(existing).toBe("merged value"); | ||
expect(storage.cached).toBe(existing); | ||
delete storage.cached; | ||
// Finish the test (success). | ||
resolve(); | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
const query = gql` | ||
query { | ||
parent { | ||
deleteMe @client | ||
} | ||
} | ||
`; | ||
|
||
function testWriteAndRead(cache: InMemoryCache) { | ||
cache.writeQuery({ | ||
query, | ||
data: { | ||
parent: { | ||
__typename: "Parent", | ||
deleteMe: "initial value", | ||
}, | ||
}, | ||
}); | ||
|
||
expect(cache.extract()).toEqual({ | ||
ROOT_QUERY: { | ||
__typename: "Query", | ||
parent: { | ||
__typename: "Parent", | ||
deleteMe: "merged value", | ||
}, | ||
}, | ||
}); | ||
|
||
expect(cache.readQuery({ query })).toEqual({ | ||
parent: { | ||
__typename: "Parent", | ||
deleteMe: "read value", | ||
}, | ||
}); | ||
} | ||
|
||
itAsync("are called when a parent object is evicted from the cache", resolve => { | ||
const cache = makeCache(resolve); | ||
testWriteAndRead(cache); | ||
|
||
const evicted = cache.evict({ | ||
// Note that we're removing Query.parent, not directly removing | ||
// Parent.deleteMe, but we still expect the Parent.deleteMe drop | ||
// function to be called. | ||
fieldName: "parent", | ||
}); | ||
expect(evicted).toBe(true); | ||
}); | ||
|
||
itAsync("are called when cache.modify causes the parent object to lose fields", resolve => { | ||
const cache = makeCache(resolve); | ||
testWriteAndRead(cache); | ||
|
||
const modified = cache.modify({ | ||
fields: { | ||
parent(value: StoreObject) { | ||
const { deleteMe, ...rest } = value; | ||
expect(rest).toEqual({ | ||
__typename: "Parent", | ||
}); | ||
return rest; | ||
}, | ||
}, | ||
}); | ||
expect(modified).toBe(true); | ||
}); | ||
|
||
itAsync("are called even if cache is cleared/restored", resolve => { | ||
const cache = makeCache(resolve); | ||
testWriteAndRead(cache); | ||
|
||
const snapshot = cache.extract(); | ||
cache.reset(); | ||
expect(cache.extract()).toEqual({}); | ||
cache.restore(snapshot); | ||
expect(cache.extract()).toEqual(snapshot); | ||
|
||
cache.writeQuery({ | ||
query, | ||
overwrite: true, | ||
data: { | ||
parent: { | ||
__typename: "Parent", | ||
deleteMe: void 0, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
itAsync("are called if merge function returns undefined", resolve => { | ||
const cache = new InMemoryCache({ | ||
typePolicies: { | ||
ToDoList: { | ||
keyFields: [], | ||
fields: { | ||
tasks: { | ||
keyArgs: false, | ||
|
||
merge(existing: number[] | undefined, incoming: number[], { args }) { | ||
if (args && args.deleteOnMerge) return; | ||
return existing ? [ | ||
...existing, | ||
...incoming, | ||
] : incoming; | ||
}, | ||
|
||
drop(existing) { | ||
expect(existing).toEqual([ | ||
{ __ref: 'Task:{"taskID":1}' }, | ||
{ __ref: 'Task:{"taskID":2}' }, | ||
{ __ref: 'Task:{"taskID":3}' }, | ||
{ __ref: 'Task:{"taskID":4}' }, | ||
]); | ||
// Finish the test (success). | ||
resolve(); | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
||
Task: { | ||
keyFields: ["taskID"], | ||
}, | ||
}, | ||
}); | ||
|
||
const query = gql` | ||
query { | ||
todoList { | ||
tasks { | ||
taskID | ||
text | ||
} | ||
} | ||
} | ||
`; | ||
|
||
const deleteQuery = gql` | ||
query { | ||
todoList { | ||
tasks(deleteOnMerge: true) { | ||
taskID | ||
text | ||
} | ||
} | ||
} | ||
`; | ||
|
||
const deleteData = { | ||
todoList: { | ||
__typename: "ToDoList", | ||
tasks: [], | ||
}, | ||
}; | ||
|
||
// This write will cause the merge function to return undefined, but | ||
// since the field is already undefined, the undefined return from the | ||
// merge function should not trigger the drop function. | ||
cache.writeQuery({ | ||
query: deleteQuery, | ||
data: deleteData, | ||
}); | ||
|
||
cache.writeQuery({ | ||
query, | ||
data: { | ||
todoList: { | ||
__typename: "ToDoList", | ||
tasks: [ | ||
{ __typename: "Task", taskID: 1, text: "task #1" }, | ||
{ __typename: "Task", taskID: 2, text: "task #2" }, | ||
], | ||
}, | ||
}, | ||
}); | ||
|
||
expect(cache.extract()).toMatchSnapshot(); | ||
|
||
cache.writeQuery({ | ||
query, | ||
data: { | ||
todoList: { | ||
__typename: "ToDoList", | ||
tasks: [ | ||
{ __typename: "Task", taskID: 3, text: "task #3" }, | ||
{ __typename: "Task", taskID: 4, text: "task #4" }, | ||
], | ||
}, | ||
}, | ||
}); | ||
|
||
expect(cache.extract()).toMatchSnapshot(); | ||
|
||
// Since the ToDoList.tasks field has data now, this deletion should | ||
// trigger the drop function, unlike the last time we used deleteQuery. | ||
cache.writeQuery({ | ||
query: deleteQuery, | ||
data: deleteData, | ||
}); | ||
}); | ||
|
||
itAsync("are called for fields within garbage collected objects", (resolve, reject) => { | ||
const cache = new InMemoryCache({ | ||
typePolicies: { | ||
Garbage: { | ||
keyFields: ["gid"], | ||
fields: { | ||
isToxic: { | ||
drop(isToxic: boolean, { readField }) { | ||
const gid = readField<number>("gid")!; | ||
if (expectedToxicities.has(gid)) { | ||
expect(expectedToxicities.get(gid)).toBe(isToxic); | ||
if (expectedToxicities.delete(gid) && | ||
expectedToxicities.size === 0) { | ||
resolve(); | ||
} | ||
} else { | ||
reject(`unexpectedly dropped garbage ${gid}`); | ||
} | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
const expectedToxicities = new Map<number, boolean>(); | ||
expectedToxicities.set(234, true); | ||
expectedToxicities.set(456, false); | ||
|
||
const query = gql` | ||
query { | ||
garbages { | ||
gid | ||
isToxic | ||
} | ||
} | ||
`; | ||
|
||
cache.writeQuery({ | ||
query, | ||
data: { | ||
garbages: [ | ||
{ __typename: "Garbage", gid: 123, isToxic: false }, | ||
{ __typename: "Garbage", gid: 234, isToxic: true }, | ||
{ __typename: "Garbage", gid: 345, isToxic: true }, | ||
{ __typename: "Garbage", gid: 456, isToxic: false }, | ||
], | ||
}, | ||
}); | ||
|
||
expect(cache.gc()).toEqual([]); | ||
|
||
cache.writeQuery({ | ||
query, | ||
overwrite: true, | ||
data: { | ||
garbages: [ | ||
{ __typename: "Garbage", gid: 123, isToxic: false }, | ||
{ __typename: "Garbage", gid: 345, isToxic: true }, | ||
], | ||
}, | ||
}); | ||
Comment on lines
+1992
to
+2001
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
expect(cache.gc().sort()).toEqual([ | ||
'Garbage:{"gid":234}', | ||
'Garbage:{"gid":456}', | ||
]); | ||
}); | ||
}); | ||
|
||
it("merge functions can deduplicate items using readField", function () { | ||
const cache = new InMemoryCache({ | ||
typePolicies: { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because of the low/central level where I'm enforcing the calling of
drop
functions, any process that uses theEntityStore
API to delete fields (or the objects that contain them) will trigger the appropriatedrop
functions. Evencache.gc()
triggersdrop
functions. Pretty cool!