Skip to content

Commit

Permalink
Merge pull request #3711 from apollographql/issue-2164
Browse files Browse the repository at this point in the history
Adjust `dataIdFromObject` logic to handle ID's with a value of 0
  • Loading branch information
hwillson authored Jul 20, 2018
2 parents 9058178 + 4cef9b9 commit e0ba906
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
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

0 comments on commit e0ba906

Please sign in to comment.