-
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
TypePolicy merge function called before cache deep merge #9503
Open
gastonmorixe
wants to merge
3
commits into
apollographql:main
Choose a base branch
from
rocketreferrals:gm/fix/merge-function-called-before-deepmerge
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.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
|
@@ -2496,6 +2496,20 @@ describe('writing to the store', () => { | |
}, | ||
}, | ||
|
||
{ | ||
existing: { | ||
__ref: "Account:12345", | ||
}, | ||
incoming: { | ||
__typename: "Account", | ||
contact: "[email protected]", | ||
id: 12345, | ||
}, | ||
merged: { | ||
__ref: "Account:12345", | ||
}, | ||
}, | ||
|
||
{ | ||
existing: { | ||
__typename: "Account", | ||
|
@@ -2923,6 +2937,157 @@ describe('writing to the store', () => { | |
}); | ||
}); | ||
|
||
it("root type policy merge is called before cache deep merge", () => { | ||
const personMergeMock = jest.fn(); | ||
|
||
let times = 0; | ||
const cache = new InMemoryCache({ | ||
typePolicies: { | ||
Person: { | ||
merge(existing, incoming, tools) { | ||
times++; | ||
|
||
personMergeMock({ | ||
times, | ||
existing, | ||
incoming, | ||
}); | ||
|
||
if (tools.isReference(existing) && !tools.isReference(incoming)) { | ||
const cachedData = tools.cache.data.lookup(existing.__ref); | ||
const existingUpdatedAt = cachedData?.["updatedAt"]; | ||
const incomingUpdatedAt = incoming?.["updatedAt"]; | ||
if ( | ||
typeof existingUpdatedAt === "number" && | ||
typeof incomingUpdatedAt === "number" && | ||
existingUpdatedAt > incomingUpdatedAt | ||
) { | ||
return existing; | ||
} | ||
} | ||
|
||
return tools.mergeObjects(existing, incoming); | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
expect(times).toEqual(0); | ||
|
||
const query = gql` | ||
query Person($offset: Int, $limit: Int) { | ||
person { | ||
id | ||
name | ||
age | ||
status | ||
updatedAt | ||
} | ||
} | ||
` | ||
|
||
expect(times).toEqual(0); | ||
|
||
cache.writeQuery({ | ||
query, | ||
data: { | ||
person: { | ||
__typename: "Person", | ||
id: 123, | ||
name: "Gaston", | ||
age: 28, | ||
status: "ACTIVE", | ||
updatedAt: 100000, | ||
}, | ||
}, | ||
variables: {}, | ||
}); | ||
|
||
expect(times).toEqual(2); // TODO: ideally this should only be called once | ||
|
||
expect(cache.extract()).toEqual({ | ||
ROOT_QUERY: { | ||
__typename: "Query", | ||
person: { | ||
__ref: "Person:123", | ||
}, | ||
}, | ||
"Person:123": { | ||
__typename: "Person", | ||
id: 123, | ||
name: "Gaston", | ||
age: 28, | ||
status: "ACTIVE", | ||
updatedAt: 100000, | ||
}, | ||
}); | ||
|
||
cache.writeQuery({ | ||
query, | ||
data: { | ||
person: { | ||
__typename: "Person", | ||
id: 123, | ||
status: "DISABLED", | ||
updatedAt: 50, | ||
}, | ||
}, | ||
variables: {}, | ||
}); | ||
|
||
expect(times).toEqual(3); | ||
|
||
expect(cache.extract()).toEqual({ | ||
ROOT_QUERY: { | ||
__typename: "Query", | ||
person: { | ||
__ref: "Person:123", | ||
}, | ||
}, | ||
"Person:123": { | ||
__typename: "Person", | ||
id: 123, | ||
name: "Gaston", | ||
age: 28, | ||
status: "ACTIVE", | ||
updatedAt: 100000, | ||
}, | ||
}); | ||
|
||
cache.writeQuery({ | ||
query, | ||
data: { | ||
person: { | ||
__typename: "Person", | ||
id: 123, | ||
status: "PENDING", | ||
updatedAt: 100001, | ||
}, | ||
}, | ||
variables: {}, | ||
}); | ||
|
||
expect(personMergeMock.mock.calls).toMatchSnapshot(); | ||
expect(times).toEqual(4); | ||
|
||
expect(cache.extract()).toEqual({ | ||
ROOT_QUERY: { | ||
__typename: "Query", | ||
person: { | ||
__ref: "Person:123", | ||
}, | ||
}, | ||
"Person:123": { | ||
__typename: "Person", | ||
id: 123, | ||
name: "Gaston", | ||
age: 28, | ||
status: "PENDING", | ||
updatedAt: 100001, | ||
}, | ||
}); | ||
}); | ||
|
||
describe("StoreWriter", () => { | ||
const writer = new StoreWriter(new InMemoryCache()); | ||
|
||
|
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
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
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 |
---|---|---|
|
@@ -154,7 +154,7 @@ export class StoreWriter { | |
context.incomingById.forEach(({ storeObject, mergeTree, fieldNodeSet }, dataId) => { | ||
const entityRef = makeReference(dataId); | ||
|
||
if (mergeTree && mergeTree.map.size) { | ||
if (mergeTree && (mergeTree.map.size || mergeTree.info)) { | ||
const applied = this.applyMerges(mergeTree, entityRef, storeObject, context); | ||
if (isReference(applied)) { | ||
// Assume References returned by applyMerges have already been merged | ||
|
@@ -421,6 +421,21 @@ export class StoreWriter { | |
previous.mergeTree = mergeMergeTrees(previous.mergeTree, mergeTree); | ||
fieldNodeSet.forEach(field => previous.fieldNodeSet.add(field)); | ||
} else { | ||
// Add the policy type's merge function for individual upcoming payloads | ||
if(typename && mergeTreeIsEmpty(mergeTree)) { | ||
const typePolicy = policies.getTypePolicy( | ||
typename, | ||
); | ||
const merge = typePolicy.merge; | ||
if (merge) { | ||
mergeTree.info = { | ||
field: undefined as any, | ||
typename, | ||
merge, | ||
}; | ||
} | ||
} | ||
|
||
context.incomingById.set(dataId, { | ||
storeObject: incoming, | ||
// Save a reference to mergeTree only if it is not empty, because | ||
|
@@ -660,6 +675,9 @@ export class StoreWriter { | |
} | ||
|
||
if (mergeTree.info) { | ||
if(isReference(existing) && isReference(incoming)){ | ||
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. may be we should check if they are the same ref too? |
||
return incoming; | ||
} | ||
return this.cache.policies.runMergeFunction( | ||
existing, | ||
incoming, | ||
|
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
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.
Not sure what field should be set here, I don't think we need a field in this case.