Skip to content

Commit

Permalink
Write field shouldn't rewrite the object reference if the field value…
Browse files Browse the repository at this point in the history
… is the same
  • Loading branch information
Slava committed Jul 18, 2016
1 parent e69d151 commit 99284ba
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/data/writeToStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,5 +281,7 @@ function writeFieldToStore({
[storeFieldName]: storeValue,
}) as StoreObject;

store[dataId] = newStoreObj;
if (!store[dataId] || storeValue !== store[dataId][storeFieldName]) {
store[dataId] = newStoreObj;
}
}
4 changes: 2 additions & 2 deletions test/optimistic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { assert } = chai;

import mockNetworkInterface from './mocks/mockNetworkInterface';
import ApolloClient, { addTypename } from '../src';
import { MutationBehaviorReducerArgs, MutationBehavior } from '../src/data/mutationResults';
import { MutationBehaviorReducerArgs, MutationBehavior, MutationQueryReducersMap } from '../src/data/mutationResults';
import { NormalizedCache, StoreObject } from '../src/data/store';

import assign = require('lodash.assign');
Expand Down Expand Up @@ -745,7 +745,7 @@ describe('optimistic mutation results', () => {
state.todoList.todos.unshift(mResult.data.createTodo);
return state;
},
};
} as MutationQueryReducersMap;
const promise = client.mutate({
mutation,
optimisticResponse,
Expand Down
36 changes: 36 additions & 0 deletions test/writeToStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import {
getIdField,
} from '../src/data/extensions';

import {
NormalizedCache,
} from '../src/data/store';

import {
Selection,
Field,
Expand Down Expand Up @@ -816,4 +820,36 @@ describe('writing to the store', () => {
});
});
});

it('does not change object references if the value is the same', () => {
const fragment = gql`
fragment Item on ItemType {
id,
stringField,
numberField,
nullField
}
`;

const result = {
id: 'abcd',
stringField: 'This is a string!',
numberField: 5,
nullField: null,
};
const store = writeFragmentToStore({
fragment,
result: _.cloneDeep(result),
});

const newStore = writeFragmentToStore({
fragment,
result: _.cloneDeep(result),
store: _.assign({}, store) as NormalizedCache,
});

Object.keys(store).forEach((field) => {
assert.equal(store[field], newStore[field], 'references are the same');
});
});
});

0 comments on commit 99284ba

Please sign in to comment.