From 07f3c69d03a4944fe0c615420d433033338197b3 Mon Sep 17 00:00:00 2001 From: doomsower Date: Thu, 17 May 2018 11:34:22 +0300 Subject: [PATCH 1/4] Add fetchMoreForQueryId safeguards --- packages/apollo-client/src/data/queries.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/apollo-client/src/data/queries.ts b/packages/apollo-client/src/data/queries.ts index 1ef69fd8e8a..bb02bf4967d 100644 --- a/packages/apollo-client/src/data/queries.ts +++ b/packages/apollo-client/src/data/queries.ts @@ -125,7 +125,7 @@ export class QueryStore { // If we have a `fetchMoreForQueryId` then we need to update the network // status for that query. See the branch for query initialization for more // explanation about this process. - if (typeof fetchMoreForQueryId === 'string') { + if (typeof fetchMoreForQueryId === 'string' && !!this.store[fetchMoreForQueryId]) { this.store[fetchMoreForQueryId].networkStatus = NetworkStatus.ready; } } @@ -143,7 +143,7 @@ export class QueryStore { // If we have a `fetchMoreForQueryId` then we need to update the network // status for that query. See the branch for query initialization for more // explanation about this process. - if (typeof fetchMoreForQueryId === 'string') { + if (typeof fetchMoreForQueryId === 'string' && !!this.store[fetchMoreForQueryId]) { this.markQueryResultClient(fetchMoreForQueryId, true); } } From 0269f8cbf71fbb42a038293cb5a0a4c4b82f4aba Mon Sep 17 00:00:00 2001 From: doomsower Date: Thu, 17 May 2018 11:42:18 +0300 Subject: [PATCH 2/4] Update changelog --- packages/apollo-client/CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/apollo-client/CHANGELOG.md b/packages/apollo-client/CHANGELOG.md index 681b13e97e1..9302272fac2 100644 --- a/packages/apollo-client/CHANGELOG.md +++ b/packages/apollo-client/CHANGELOG.md @@ -6,6 +6,10 @@ (Android), by instead setting the `prototype` of `this` manually. [Issue #3236](https://github.com/apollographql/apollo-client/issues/3236) [PR #3306](https://github.com/apollographql/apollo-client/pull/3306) +- Added safety checks in case when query component gets unmounted while `fetchMore` is in flight + (Android), by instead setting the `prototype` of `this` manually. + [Issue #3466](https://github.com/apollographql/apollo-client/issues/3466) + [PR #3469](https://github.com/apollographql/apollo-client/pull/3469) ### 2.3.0 - fixed edge case bug of changing fetchPolicies right after resetStore with no variables present From 1d379c37bbfaf8ade337d2bc51a361ac5a4b8e60 Mon Sep 17 00:00:00 2001 From: Hugh Willson Date: Thu, 24 May 2018 21:41:27 -0400 Subject: [PATCH 3/4] Add `markQueryError` tests; Removed un-needed safeguard --- .../src/data/__tests__/queries.ts | 21 +++++++++++++++++++ packages/apollo-client/src/data/queries.ts | 5 +---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/packages/apollo-client/src/data/__tests__/queries.ts b/packages/apollo-client/src/data/__tests__/queries.ts index 297d49af806..2f04c5090df 100644 --- a/packages/apollo-client/src/data/__tests__/queries.ts +++ b/packages/apollo-client/src/data/__tests__/queries.ts @@ -72,4 +72,25 @@ describe('QueryStore', () => { }, ); }); + + describe('markQueryError', () => { + it( + 'should set the network status of a `fetchMoreForQueryId` query to ' + + '`ready` in the store, if it exists', + () => { + queryStore.markQueryError(queryId, null, queryId); + expect(queryStore.get(queryId).networkStatus).toBe(NetworkStatus.ready); + }, + ); + + it( + 'should not attempt to set the network status of a ' + + '`fetchMoreForQueryId` query, if it does not exist in the store', + () => { + expect(() => { + queryStore.markQueryError(queryId, null, 'id-does-not-exist'); + }).not.toThrow("Cannot set property 'networkStatus' of undefined"); + }, + ); + }); }); diff --git a/packages/apollo-client/src/data/queries.ts b/packages/apollo-client/src/data/queries.ts index 32f1e9d076d..35264114f8d 100644 --- a/packages/apollo-client/src/data/queries.ts +++ b/packages/apollo-client/src/data/queries.ts @@ -149,10 +149,7 @@ export class QueryStore { // If we have a `fetchMoreForQueryId` then we need to update the network // status for that query. See the branch for query initialization for more // explanation about this process. - if ( - typeof fetchMoreForQueryId === 'string' && - this.store[fetchMoreForQueryId] - ) { + if (typeof fetchMoreForQueryId === 'string') { this.markQueryResultClient(fetchMoreForQueryId, true); } } From 076832f03a9973f9dfdb74638c4d99940f85909a Mon Sep 17 00:00:00 2001 From: Hugh Willson Date: Thu, 24 May 2018 21:45:39 -0400 Subject: [PATCH 4/4] Remove mention of `markQueryError` safeguards --- packages/apollo-client/CHANGELOG.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/apollo-client/CHANGELOG.md b/packages/apollo-client/CHANGELOG.md index 2b02a691dec..af9ad43f614 100644 --- a/packages/apollo-client/CHANGELOG.md +++ b/packages/apollo-client/CHANGELOG.md @@ -6,11 +6,11 @@ (Android), by instead setting the `prototype` of `this` manually. [Issue #3236](https://github.com/apollographql/apollo-client/issues/3236) [PR #3306](https://github.com/apollographql/apollo-client/pull/3306) -- Added safeguards to make sure `QueryStore.initQuery`, - `QueryStore.markQueryResult`, and `QueryStore.markQueryError` don't try to - set the network status of a `fetchMoreForQueryId` query, if it does not exist - in the store. This was happening when a query component was unmounted while - a `fetchMore` was still in flight. +- Added safeguards to make sure `QueryStore.initQuery` and + `QueryStore.markQueryResult` don't try to set the network status of a + `fetchMoreForQueryId` query, if it does not exist in the store. This was + happening when a query component was unmounted while a `fetchMore` was still + in flight. [Issue #3345](https://github.com/apollographql/apollo-client/issues/3345) [Issue #3466](https://github.com/apollographql/apollo-client/issues/3466) [PR #3367](https://github.com/apollographql/apollo-client/pull/3367)