From 062079c7f3970280a12959e9878b636184a4f894 Mon Sep 17 00:00:00 2001 From: Ben Newman Date: Thu, 23 Sep 2021 10:58:26 -0400 Subject: [PATCH] Stop excluding observerless queries from `refetchQueries` selection (#8825) --- CHANGELOG.md | 3 ++ src/__tests__/refetchQueries.ts | 76 +++++++++++++++++++++++++++++++++ src/core/QueryManager.ts | 6 ++- 3 files changed, 83 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 651c6168a31..f88439d8e61 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ - Clear `InMemoryCache` `watches` set when `cache.reset()` called.
[@benjamn](https://github.com/benjamn) in [#8826](https://github.com/apollographql/apollo-client/pull/8826) +- Stop excluding observerless queries from `refetchQueries: [...]` selection.
+ [@benjamn](https://github.com/benjamn) in [#8825](https://github.com/apollographql/apollo-client/pull/8825) + ## Apollo Client 3.4.13 ### Bug Fixes diff --git a/src/__tests__/refetchQueries.ts b/src/__tests__/refetchQueries.ts index a18f8f5a861..8bc918f8f64 100644 --- a/src/__tests__/refetchQueries.ts +++ b/src/__tests__/refetchQueries.ts @@ -521,6 +521,82 @@ describe("client.refetchQueries", () => { resolve(); }); + itAsync('includes queries named in refetchQueries even if they have no observers', async (resolve, reject) => { + const client = makeClient(); + + const aObs = client.watchQuery({ query: aQuery }); + const bObs = client.watchQuery({ query: bQuery }); + const abObs = client.watchQuery({ query: abQuery }); + + // These ObservableQuery objects have no observers yet, but should + // nevertheless be refetched if identified explicitly in an options.include + // array passed to client.refetchQueries. + expect(aObs.hasObservers()).toBe(false); + expect(bObs.hasObservers()).toBe(false); + expect(abObs.hasObservers()).toBe(false); + + const activeResults = await client.refetchQueries({ + include: ["A", abQuery], + + onQueryUpdated(obs, diff) { + if (obs === aObs) { + expect(diff.complete).toBe(false); + expect(diff.result).toEqual({}); + } else if (obs === abObs) { + expect(diff.complete).toBe(false); + expect(diff.result).toEqual({}); + } else { + reject(`unexpected ObservableQuery ${ + obs.queryId + } with name ${obs.queryName}`); + } + return Promise.resolve(diff.result); + }, + }); + + sortObjects(activeResults); + expect(activeResults).toEqual([ + {}, + {}, + ]); + + subs.push(abObs.subscribe({ + next(result) { + expect(result.data).toEqual({ a: "A", b: "B" }); + + client.refetchQueries({ + include: [aQuery, "B"], + + onQueryUpdated(obs, diff) { + if (obs === aObs) { + expect(diff.result).toEqual({ a: "A" }); + } else if (obs === bObs) { + expect(diff.result).toEqual({ b: "B" }); + } else if (obs === abObs) { + expect(diff.result).toEqual({ a: "A", b: "B" }); + } else { + reject(`unexpected ObservableQuery ${ + obs.queryId + } with name ${obs.queryName}`); + } + return Promise.resolve(diff.result); + }, + + }).then(resultsAfterSubscribe => { + sortObjects(resultsAfterSubscribe); + expect(resultsAfterSubscribe).toEqual([ + { a: "A" }, + { b: "B" }, + ]); + + unsubscribe(); + }).then(resolve, reject); + }, + })); + + expect(abObs.hasObservers()).toBe(true); + }); + itAsync('should not include unwatched single queries', async (resolve, reject) => { const client = makeClient(); const [ diff --git a/src/core/QueryManager.ts b/src/core/QueryManager.ts index 312ca8eb4a9..e09911cc384 100644 --- a/src/core/QueryManager.ts +++ b/src/core/QueryManager.ts @@ -767,8 +767,10 @@ export class QueryManager { options: { fetchPolicy }, } = oq; - if (fetchPolicy === "standby" || !oq.hasObservers()) { - // Skip inactive queries unless include === "all". + if ( + fetchPolicy === "standby" || + (include === "active" && !oq.hasObservers()) + ) { return; }