From 9c578335a32dde5084a12da67477c54bb60ad7c0 Mon Sep 17 00:00:00 2001 From: doomsower Date: Thu, 14 Feb 2019 00:29:03 +0300 Subject: [PATCH 1/2] remove unused queryId after fetchMore --- .../apollo-client/src/__tests__/fetchMore.ts | 35 +++++++++++++++++++ .../apollo-client/src/core/ObservableQuery.ts | 4 ++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/packages/apollo-client/src/__tests__/fetchMore.ts b/packages/apollo-client/src/__tests__/fetchMore.ts index f196e2e45ef..602004cff3c 100644 --- a/packages/apollo-client/src/__tests__/fetchMore.ts +++ b/packages/apollo-client/src/__tests__/fetchMore.ts @@ -473,6 +473,41 @@ describe('fetchMore on an observable query', () => { }, }); }); + + it('will not leak fetchMore query', () => { + latestResult = null; + var beforeQueryCount; + return setup({ + request: { + query, + variables: variablesMore, + }, + result: resultMore, + }) + .then(watchedQuery => { + beforeQueryCount = Object.keys( + client.queryManager.queryStore.getStore(), + ).length; + return watchedQuery.fetchMore({ + variables: { start: 10 }, // rely on the fact that the original variables had limit: 10 + updateQuery: (prev, options) => { + const state = cloneDeep(prev) as any; + state.entry.comments = [ + ...state.entry.comments, + ...(options.fetchMoreResult as any).entry.comments, + ]; + return state; + }, + }); + }) + .then(data => { + var afterQueryCount = Object.keys( + client.queryManager.queryStore.getStore(), + ).length; + expect(afterQueryCount).toBe(beforeQueryCount); + unsetup(); + }); + }); }); describe('fetchMore on an observable query with connection', () => { diff --git a/packages/apollo-client/src/core/ObservableQuery.ts b/packages/apollo-client/src/core/ObservableQuery.ts index b39cb647d42..7d61da4787e 100644 --- a/packages/apollo-client/src/core/ObservableQuery.ts +++ b/packages/apollo-client/src/core/ObservableQuery.ts @@ -343,10 +343,11 @@ export class ObservableQuery< ); let combinedOptions: any; + let qid: string; return Promise.resolve() .then(() => { - const qid = this.queryManager.generateQueryId(); + qid = this.queryManager.generateQueryId(); if (fetchMoreOptions.query) { // fetch a new query @@ -380,6 +381,7 @@ export class ObservableQuery< variables: combinedOptions.variables, }), ); + this.queryManager.stopQuery(qid); return fetchMoreResult as ApolloQueryResult; }); From 6d5078bdb5ca542bf933f73136a91f7ff474f6fa Mon Sep 17 00:00:00 2001 From: Ben Newman Date: Mon, 8 Apr 2019 20:04:16 -0400 Subject: [PATCH 2/2] Simplify fetchMore cleanup logic. --- CHANGELOG.md | 3 +++ packages/apollo-client/src/core/ObservableQuery.ts | 9 ++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b537c6f270f..7353ae2483e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,9 @@ - Avoid updating (and later invalidating) cache watches when `fetchPolicy` is `'no-cache'`.
[@bradleyayers](https://github.com/bradleyayers) in [PR #4573](https://github.com/apollographql/apollo-client/pull/4573), part of [issue #3452](https://github.com/apollographql/apollo-client/issues/3452) +- Remove temporary `queryId` after `fetchMore` completes.
+ [@doomsower](https://github.com/doomsower) in [#4440](https://github.com/apollographql/apollo-client/pull/4440) + ### Apollo Cache In-Memory - Support `new InMemoryCache({ freezeResults: true })` to help enforce immutability.
diff --git a/packages/apollo-client/src/core/ObservableQuery.ts b/packages/apollo-client/src/core/ObservableQuery.ts index 7d61da4787e..fd54b11d745 100644 --- a/packages/apollo-client/src/core/ObservableQuery.ts +++ b/packages/apollo-client/src/core/ObservableQuery.ts @@ -343,12 +343,10 @@ export class ObservableQuery< ); let combinedOptions: any; - let qid: string; + const qid = this.queryManager.generateQueryId(); return Promise.resolve() .then(() => { - qid = this.queryManager.generateQueryId(); - if (fetchMoreOptions.query) { // fetch a new query combinedOptions = fetchMoreOptions; @@ -381,9 +379,14 @@ export class ObservableQuery< variables: combinedOptions.variables, }), ); + this.queryManager.stopQuery(qid); return fetchMoreResult as ApolloQueryResult; + + }, error => { + this.queryManager.stopQuery(qid); + throw error; }); }