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

Preserve referential equality from previous result in the store #1136

Merged
merged 21 commits into from
Jan 9, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Expect active development and potentially significant breaking changes in the `0
- Remove lodash as a production dependency [PR #1122](https://github.com/apollostack/apollo-client/pull/1122)
- Minor fix to write to `ROOT_SUBSCRIPTION` ID in the store for subscription results. [PR #1122](https://github.com/apollostack/apollo-client/pull/1127)
- Remove `whatwg-fetch` polyfill dependency and instead warn when a global `fetch` implementation is not found. [PR #1134](https://github.com/apollostack/apollo-client/pull/1134)
- New results from `watchQuery` are referentially equal (so `a === b`) to previous results if nothing changed in the store for a better UI integration experience when determining what changed. [PR #1136](https://github.com/apollostack/apollo-client/pull/1136)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This misses the important point that this applies to sub-objects in the result, not just the whole result (which was true before!)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed 👍


### 0.6.0
- Switch to `@types/graphql` instead of `typed-graphql` for typings. [PR 1041](https://github.com/apollostack/apollo-client/pull/1041) [PR #934](https://github.com/apollostack/apollo-client/issues/934)
Expand Down
90 changes: 88 additions & 2 deletions test/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,14 @@ describe('QueryManager', () => {
request,
firstResult,
secondResult,
thirdResult,
}: {
request: ParsedRequest,
firstResult: ExecutionResult,
secondResult: ExecutionResult,
thirdResult?: ExecutionResult,
}) => {
return mockQueryManager(
const args = [
{
request,
result: firstResult,
Expand All @@ -218,7 +220,13 @@ describe('QueryManager', () => {
request,
result: secondResult,
},
);
];

if (thirdResult) {
args.push({ request, result: thirdResult });
}

return mockQueryManager(...args);
};

it('properly roundtrips through a Redux store', (done) => {
Expand Down Expand Up @@ -664,6 +672,84 @@ describe('QueryManager', () => {
);
});

it('will return referentially equivalent data if nothing changed in a refetch', done => {
const request = {
query: gql`
{
a
b { c }
d { e f { g } }
}
`,
};

const data1 = {
a: 1,
b: { c: 2 },
d: { e: 3, f: { g: 4 } },
};

const data2 = {
a: 1,
b: { c: 2 },
d: { e: 30, f: { g: 4 } },
};

const data3 = {
a: 1,
b: { c: 2 },
d: { e: 3, f: { g: 4 } },
};

const queryManager = mockRefetch({
request,
firstResult: { data: data1 },
secondResult: { data: data2 },
thirdResult: { data: data3 },
});

const observable = queryManager.watchQuery<any>(request);

let count = 0;
let firstResultData: any;

observable.subscribe({
next: result => {
try {
switch (count++) {
case 0:
assert.deepEqual(result.data, data1);
firstResultData = result.data;
observable.refetch();
break;
case 1:
assert.deepEqual(result.data, data2);
assert.notStrictEqual(result.data, firstResultData);
assert.strictEqual(result.data.b, firstResultData.b);
assert.notStrictEqual(result.data.d, firstResultData.d);
assert.strictEqual(result.data.d.f, firstResultData.d.f);
observable.refetch();
break;
case 2:
assert.deepEqual(result.data, data3);
assert.notStrictEqual(result.data, firstResultData);
assert.strictEqual(result.data.b, firstResultData.b);
assert.notStrictEqual(result.data.d, firstResultData.d);
assert.strictEqual(result.data.d.f, firstResultData.d.f);
done();
break;
default:
throw new Error('Next run too many times.');
}
} catch (error) {
done(error);
}
},
error: error =>
done(error),
});
});

it('sets networkStatus to `refetch` when refetching', () => {
const request = {
query: gql`
Expand Down