Skip to content

Commit

Permalink
Fix cache override warning output (#11483)
Browse files Browse the repository at this point in the history
  • Loading branch information
pipopotamasu committed Jan 17, 2024
1 parent abfd02a commit 6394dda
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/spotty-bats-march.md
@@ -0,0 +1,5 @@
---
"@apollo/client": patch
---

Fix cache override warning output
67 changes: 64 additions & 3 deletions src/cache/inmemory/__tests__/__snapshots__/writeToStore.ts.snap
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`writing to the store "Cache data maybe lost..." warnings should not warn when scalar fields are updated 1`] = `
exports[`writing to the store "Cache data may be lost..." warnings should not warn when scalar fields are updated 1`] = `
Object {
"ROOT_QUERY": Object {
"__typename": "Query",
Expand All @@ -18,7 +18,7 @@ Object {
}
`;
exports[`writing to the store "Cache data maybe lost..." warnings should not warn when scalar fields are updated 2`] = `
exports[`writing to the store "Cache data may be lost..." warnings should not warn when scalar fields are updated 2`] = `
Object {
"ROOT_QUERY": Object {
"__typename": "Query",
Expand All @@ -34,7 +34,68 @@ Object {
}
`;
exports[`writing to the store "Cache data maybe lost..." warnings should not warn when scalar fields are updated 3`] = `[MockFunction]`;
exports[`writing to the store "Cache data may be lost..." warnings should not warn when scalar fields are updated 3`] = `[MockFunction]`;
exports[`writing to the store "Cache data may be lost..." warnings should warn "Cache data may be lost..." message 1`] = `
Object {
"ROOT_QUERY": Object {
"__typename": "Query",
"someJSON": Object {
"name": "Tom",
},
},
}
`;
exports[`writing to the store "Cache data may be lost..." warnings should warn "Cache data may be lost..." message 2`] = `
Object {
"ROOT_QUERY": Object {
"__typename": "Query",
"someJSON": Object {
"age": 20,
},
},
}
`;
exports[`writing to the store "Cache data may be lost..." warnings should warn "Cache data may be lost..." message 3`] = `
[MockFunction] {
"calls": Array [
Array [
"Cache data may be lost when replacing the %s field of a %s object.
This could cause additional (usually avoidable) network requests to fetch data that were otherwise cached.
To address this problem (which is not a bug in Apollo Client), %sdefine a custom merge function for the %s field, so InMemoryCache can safely merge these objects:
existing: %o
incoming: %o
For more information about these options, please refer to the documentation:
* Ensuring entity objects have IDs: https://go.apollo.dev/c/generating-unique-identifiers
* Defining custom merge functions: https://go.apollo.dev/c/merging-non-normalized-objects
",
"someJSON",
"Query",
"",
"Query.someJSON",
Object {
"name": "Tom",
},
Object {
"age": 20,
},
],
],
"results": Array [
Object {
"type": "return",
"value": undefined,
},
],
}
`;
exports[`writing to the store correctly merges fragment fields along multiple paths 1`] = `
Object {
Expand Down
38 changes: 37 additions & 1 deletion src/cache/inmemory/__tests__/writeToStore.ts
Expand Up @@ -2047,7 +2047,43 @@ describe("writing to the store", () => {
});
});

describe('"Cache data maybe lost..." warnings', () => {
describe('"Cache data may be lost..." warnings', () => {
it('should warn "Cache data may be lost..." message', () => {
using _consoleSpy = spyOnConsole.takeSnapshots("warn");
const cache = new InMemoryCache();

const query = gql`
query {
someJSON {
name
age
}
}
`;

cache.writeQuery({
query,
data: {
someJSON: {
name: "Tom",
},
},
});

expect(cache.extract()).toMatchSnapshot();

cache.writeQuery({
query,
data: {
someJSON: {
age: 20,
},
},
});

expect(cache.extract()).toMatchSnapshot();
});

it("should not warn when scalar fields are updated", () => {
using _consoleSpy = spyOnConsole.takeSnapshots("warn");
const cache = new InMemoryCache();
Expand Down
4 changes: 2 additions & 2 deletions src/cache/inmemory/writeToStore.ts
Expand Up @@ -857,8 +857,8 @@ This could cause additional (usually avoidable) network requests to fetch data t
To address this problem (which is not a bug in Apollo Client), %sdefine a custom merge function for the %s field, so InMemoryCache can safely merge these objects:
existing: %s
incoming: %s
existing: %o
incoming: %o
For more information about these options, please refer to the documentation:
Expand Down

0 comments on commit 6394dda

Please sign in to comment.