Skip to content

Commit

Permalink
Allow refetchQueries to refetch observerless queries (again).
Browse files Browse the repository at this point in the history
This PR is a follow-up to PR #8825, which was first released in Apollo
Client v3.4.14. As a number of commenters have reported in issue #5419,
it appears we did not completely fix the problem, so this PR attempts to
finish that work.

Since ObservableQuery redelivers the most recent result/error to new
subscribers, it is not completely pointless to refetch an
ObservableQuery that (currently) has no subscribers.

However, putting the ObservableQuery in "standby" mode by setting the
obsQuery.options.fetchPolicy to "standby" should still exclude the
ObservableQuery from "active" status as far as refetchQueries is
concerned. This logic handles useLazyQuery queries that have not yet
been executed, since they start out in "standby" mode until called.
  • Loading branch information
benjamn committed Apr 21, 2022
1 parent 85e8fc3 commit fcdedd0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/__tests__/refetchQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,8 @@ describe("client.refetchQueries", () => {
expect(diff.result).toEqual({ b: "B" });
} else if (obs === abObs) {
expect(diff.result).toEqual({ a: "A", b: "B" });
} else if (obs === extraObs) {
expect(diff.result).toEqual({ a: "A", b: "B" });
} else {
reject(`unexpected ObservableQuery ${
obs.queryId
Expand All @@ -475,6 +477,7 @@ describe("client.refetchQueries", () => {
expect(activeResults).toEqual([
{ a: "A" },
{ a: "A", b: "B" },
{ a: "A", b: "B" },
{ b: "B" },
]);

Expand Down
5 changes: 1 addition & 4 deletions src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -765,10 +765,7 @@ export class QueryManager<TStore> {
options: { fetchPolicy },
} = oq;

if (
fetchPolicy === "standby" ||
(include === "active" && !oq.hasObservers())
) {
if (fetchPolicy === "standby") {
return;
}

Expand Down
8 changes: 5 additions & 3 deletions src/core/__tests__/QueryManager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3742,7 +3742,7 @@ describe('QueryManager', () => {
}, 50);
});

itAsync('should not call refetch on a non-subscribed Observable if the store is reset', (resolve, reject) => {
itAsync('should not call refetch on a standby Observable if the store is reset', (resolve, reject) => {
const query = gql`
query {
author {
Expand All @@ -3758,6 +3758,7 @@ describe('QueryManager', () => {

const options = {
query,
fetchPolicy: "standby",
} as WatchQueryOptions;

let refetchCount = 0;
Expand Down Expand Up @@ -4225,7 +4226,7 @@ describe('QueryManager', () => {
}, 50);
});

itAsync('should not call refetch on a non-subscribed Observable', (resolve, reject) => {
itAsync('should not call refetch on a standby Observable', (resolve, reject) => {
const query = gql`
query {
author {
Expand All @@ -4240,7 +4241,8 @@ describe('QueryManager', () => {
});

const options = {
query
query,
fetchPolicy: "standby",
} as WatchQueryOptions;

let refetchCount = 0;
Expand Down

0 comments on commit fcdedd0

Please sign in to comment.