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

Adjust dataIdFromObject logic to handle ID's with a value of 0 #3711

Merged
merged 2 commits into from
Jul 20, 2018
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
- Add `__typename` and `id` properties to `dataIdFromObject` parameter
(typescript)
[@jfurler](https://github.com/jfurler) in [#3641](https://github.com/apollographql/apollo-client/pull/3641)
- Fixed an issue caused by `dataIdFromObject` considering returned 0 values to
be falsy, instead of being a valid ID, which lead to the store not being
updated properly in some cases.
[@hwillson](https://github.com/hwillson) in [#3711](https://github.com/apollographql/apollo-client/pull/3711)

## 2.3.5 (June 19, 2018)

Expand Down
42 changes: 42 additions & 0 deletions packages/apollo-cache-inmemory/src/__tests__/writeToStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1207,6 +1207,48 @@ describe('writing to the store', () => {
});
});

it('should write to store if `dataIdFromObject` returns an ID of 0', () => {
const query = gql`
query {
author {
firstName
id
__typename
}
}
`;
const data = {
author: {
id: 0,
__typename: 'Author',
firstName: 'John',
},
};
const expStore = defaultNormalizedCacheFactory({
ROOT_QUERY: {
author: {
id: 0,
typename: 'Author',
type: 'id',
generated: false,
},
},
0: {
id: data.author.id,
__typename: data.author.__typename,
firstName: data.author.firstName,
},
});

expect(
writeQueryToStore({
result: data,
query,
dataIdFromObject: () => 0,
}).toObject(),
).toEqual(expStore.toObject());
});

describe('type escaping', () => {
const dataIdFromObject = (object: any) => {
if (object.__typename && object.id) {
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-cache-inmemory/src/writeToStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ function writeFieldToStore({
);
}

if (semanticId) {
if (semanticId || (typeof semanticId === 'number' && semanticId === 0)) {
valueDataId = semanticId;
generated = false;
}
Expand Down