-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Run Policies's merge before cache merge
adds test fix formatting
- Loading branch information
1 parent
6ca525a
commit 045a9d5
Showing
8 changed files
with
251 additions
and
6 deletions.
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
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