-
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.
Merge pull request #7439 from apollographql/canonical-cache-results
Efficiently canonicalize InMemoryCache result objects.
- Loading branch information
Showing
16 changed files
with
511 additions
and
67 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
18 changes: 18 additions & 0 deletions
18
src/cache/inmemory/__tests__/__snapshots__/readFromStore.ts.snap
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`reading from the store returns === results for different queries 1`] = ` | ||
Object { | ||
"ROOT_QUERY": Object { | ||
"__typename": "Query", | ||
"a": Array [ | ||
"a", | ||
"y", | ||
"y", | ||
], | ||
"b": Object { | ||
"c": "C", | ||
"d": "D", | ||
}, | ||
}, | ||
} | ||
`; |
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 |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { ObjectCanon } from "../object-canon"; | ||
|
||
describe("ObjectCanon", () => { | ||
it("can canonicalize objects and arrays", () => { | ||
const canon = new ObjectCanon; | ||
|
||
const obj1 = { | ||
a: [1, 2], | ||
b: { | ||
c: [{ | ||
d: "dee", | ||
e: "ee", | ||
}, "f"], | ||
g: "gee", | ||
}, | ||
}; | ||
|
||
const obj2 = { | ||
b: { | ||
g: "gee", | ||
c: [{ | ||
e: "ee", | ||
d: "dee", | ||
}, "f"], | ||
}, | ||
a: [1, 2], | ||
}; | ||
|
||
expect(obj1).toEqual(obj2); | ||
expect(obj1).not.toBe(obj2); | ||
|
||
const c1 = canon.admit(obj1); | ||
const c2 = canon.admit(obj2); | ||
|
||
expect(c1).toBe(c2); | ||
expect(c1).toEqual(obj1); | ||
expect(c1).toEqual(obj2); | ||
expect(c2).toEqual(obj1); | ||
expect(c2).toEqual(obj2); | ||
expect(c1).not.toBe(obj1); | ||
expect(c1).not.toBe(obj2); | ||
expect(c2).not.toBe(obj1); | ||
expect(c2).not.toBe(obj2); | ||
|
||
expect(canon.admit(c1)).toBe(c1); | ||
expect(canon.admit(c2)).toBe(c2); | ||
}); | ||
|
||
it("preserves custom prototypes", () => { | ||
const canon = new ObjectCanon; | ||
|
||
class Custom { | ||
constructor(public value: any) {} | ||
getValue() { return this.value } | ||
} | ||
|
||
const customs = [ | ||
new Custom("oyez"), | ||
new Custom(1234), | ||
new Custom(true), | ||
]; | ||
|
||
const admitted = canon.admit(customs); | ||
expect(admitted).not.toBe(customs); | ||
expect(admitted).toEqual(customs); | ||
|
||
function check(i: number) { | ||
expect(admitted[i]).toEqual(customs[i]); | ||
expect(admitted[i]).not.toBe(customs[i]); | ||
expect(admitted[i].getValue()).toBe(customs[i].getValue()); | ||
expect(Object.getPrototypeOf(admitted[i])).toBe(Custom.prototype); | ||
expect(admitted[i]).toBeInstanceOf(Custom); | ||
} | ||
check(0); | ||
check(1); | ||
check(2); | ||
|
||
expect(canon.admit(customs)).toBe(admitted); | ||
}); | ||
|
||
it("unwraps Pass wrappers as-is", () => { | ||
const canon = new ObjectCanon; | ||
|
||
const cd = { | ||
c: "see", | ||
d: "dee", | ||
}; | ||
|
||
const obj = { | ||
a: cd, | ||
b: canon.pass(cd), | ||
e: cd, | ||
}; | ||
|
||
function check() { | ||
const admitted = canon.admit(obj); | ||
expect(admitted).not.toBe(obj); | ||
expect(admitted.b).toBe(cd); | ||
expect(admitted.e).toEqual(cd); | ||
expect(admitted.e).not.toBe(cd); | ||
expect(admitted.e).toEqual(admitted.b); | ||
expect(admitted.e).not.toBe(admitted.b); | ||
expect(admitted.e).toBe(admitted.a); | ||
} | ||
check(); | ||
check(); | ||
}); | ||
}); |
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
Oops, something went wrong.