Skip to content

Commit

Permalink
Stop excluding observerless queries from refetchQueries selection (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn authored Sep 23, 2021
1 parent 372eb8f commit 062079c
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
- Clear `InMemoryCache` `watches` set when `cache.reset()` called. <br/>
[@benjamn](https://github.com/benjamn) in [#8826](https://github.com/apollographql/apollo-client/pull/8826)

- Stop excluding observerless queries from `refetchQueries: [...]` selection. <br/>
[@benjamn](https://github.com/benjamn) in [#8825](https://github.com/apollographql/apollo-client/pull/8825)

## Apollo Client 3.4.13

### Bug Fixes
Expand Down
76 changes: 76 additions & 0 deletions src/__tests__/refetchQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down
6 changes: 4 additions & 2 deletions src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -767,8 +767,10 @@ export class QueryManager<TStore> {
options: { fetchPolicy },
} = oq;

if (fetchPolicy === "standby" || !oq.hasObservers()) {
// Skip inactive queries unless include === "all".
if (
fetchPolicy === "standby" ||
(include === "active" && !oq.hasObservers())
) {
return;
}

Expand Down

0 comments on commit 062079c

Please sign in to comment.