Skip to content
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

Warn when clobbering non-normalized data in the cache. #6372

Merged
merged 6 commits into from
Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@
- Cache methods that would normally trigger a broadcast, like `cache.evict`, `cache.writeQuery`, and `cache.writeFragment`, can now be called with a named options object, which supports a `broadcast: boolean` property that can be used to silence the broadcast, for situations where you want to update the cache multiple times without triggering a broadcast each time. <br/>
[@benjamn](https://github.com/benjamn) in [#6288](https://github.com/apollographql/apollo-client/pull/6288)

- `InMemoryCache` now `console.warn`s in development whenever non-normalized data is dangerously overwritten, with helpful links to documentation about normalization and custom `merge` functions. <br/>
[@benjamn](https://github.com/benjamn) in [#6372](https://github.com/apollographql/apollo-client/pull/6372)

- The contents of the `@apollo/react-hooks` package have been merged into `@apollo/client`, enabling the following all-in-one `import`:
```ts
import { ApolloClient, ApolloProvider, useQuery } from '@apollo/client';
Expand Down
30 changes: 29 additions & 1 deletion src/__tests__/ApolloClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,21 @@ describe('ApolloClient', () => {
it('will write some deeply nested data to the store', () => {
const client = new ApolloClient({
link: ApolloLink.empty(),
cache: new InMemoryCache(),
cache: new InMemoryCache({
typePolicies: {
Query: {
fields: {
d: {
// Silence "Cache data may be lost..." warnings by
// unconditionally favoring the incoming data.
merge(_, incoming) {
return incoming;
},
},
},
},
},
}),
});

client.writeQuery({
Expand Down Expand Up @@ -1171,6 +1185,20 @@ describe('ApolloClient', () => {
return new ApolloClient({
link,
cache: new InMemoryCache({
typePolicies: {
Person: {
fields: {
friends: {
// Deliberately silence "Cache data may be lost..."
// warnings by preferring the incoming data, rather
// than (say) concatenating the arrays together.
merge(_, incoming) {
return incoming;
},
},
},
},
},
dataIdFromObject: result => {
if (result.id && result.__typename) {
return result.__typename + result.id;
Expand Down
14 changes: 14 additions & 0 deletions src/__tests__/optimistic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,20 @@ describe('optimistic mutation results', () => {
const client = new ApolloClient({
link,
cache: new InMemoryCache({
typePolicies: {
TodoList: {
fields: {
todos: {
// Deliberately silence "Cache data may be lost..."
// warnings by favoring the incoming data, rather than
// (say) concatenating the arrays together.
merge(_, incoming) {
return incoming;
},
},
},
},
},
dataIdFromObject: (obj: any) => {
if (obj.id && obj.__typename) {
return obj.__typename + obj.id;
Expand Down
187 changes: 99 additions & 88 deletions src/cache/inmemory/__tests__/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ describe('Cache', () => {
];

cachesList.forEach((caches, i) => {
it(message + ` (${i + 1}/${cachesList.length})`, () =>
callback(...caches),
);
it(`${message} (${i + 1}/${cachesList.length})`,
() => callback(...caches));
});
}

Expand Down Expand Up @@ -869,106 +868,118 @@ describe('Cache', () => {
});
});

itWithInitialData(
'will write some deeply nested data to the store',
[{}],
proxy => {
proxy.writeQuery({
data: { a: 1, d: { e: 4 } },
query: gql`
{
a
d {
e
}
it('will write some deeply nested data to the store', () => {
const cache = new InMemoryCache({
typePolicies: {
Query: {
fields: {
d: {
// Deliberately silence "Cache data may be lost..."
// warnings by unconditionally favoring the incoming data.
merge(_, incoming) {
return incoming;
},
},
},
},
},
});

cache.writeQuery({
data: { a: 1, d: { e: 4 } },
query: gql`
{
a
d {
e
}
`,
});
}
`,
});

expect((proxy as InMemoryCache).extract()).toEqual({
ROOT_QUERY: {
__typename: "Query",
a: 1,
d: {
e: 4,
},
expect((cache as InMemoryCache).extract()).toEqual({
ROOT_QUERY: {
__typename: "Query",
a: 1,
d: {
e: 4,
},
});
},
});

proxy.writeQuery({
data: { a: 1, d: { h: { i: 7 } } },
query: gql`
{
a
d {
h {
i
}
cache.writeQuery({
data: { a: 1, d: { h: { i: 7 } } },
query: gql`
{
a
d {
h {
i
}
}
`,
});
}
`,
});

expect((proxy as InMemoryCache).extract()).toEqual({
ROOT_QUERY: {
__typename: "Query",
a: 1,
// The new value for d overwrites the old value, since there
// is no custom merge function defined for Query.d.
d: {
h: {
i: 7,
},
expect((cache as InMemoryCache).extract()).toEqual({
ROOT_QUERY: {
__typename: "Query",
a: 1,
// The new value for d overwrites the old value, since there
// is no custom merge function defined for Query.d.
d: {
h: {
i: 7,
},
},
});
},
});

proxy.writeQuery({
data: {
a: 1,
b: 2,
c: 3,
d: { e: 4, f: 5, g: 6, h: { i: 7, j: 8, k: 9 } },
},
query: gql`
{
a
b
c
d {
e
f
g
h {
i
j
k
}
cache.writeQuery({
data: {
a: 1,
b: 2,
c: 3,
d: { e: 4, f: 5, g: 6, h: { i: 7, j: 8, k: 9 } },
},
query: gql`
{
a
b
c
d {
e
f
g
h {
i
j
k
}
}
`,
});
}
`,
});

expect((proxy as InMemoryCache).extract()).toEqual({
ROOT_QUERY: {
__typename: "Query",
a: 1,
b: 2,
c: 3,
d: {
e: 4,
f: 5,
g: 6,
h: {
i: 7,
j: 8,
k: 9,
},
expect((cache as InMemoryCache).extract()).toEqual({
ROOT_QUERY: {
__typename: "Query",
a: 1,
b: 2,
c: 3,
d: {
e: 4,
f: 5,
g: 6,
h: {
i: 7,
j: 8,
k: 9,
},
},
});
},
);
},
});
});

itWithInitialData(
'will write some data to the store with variables',
Expand Down
Loading