Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop excluding observerless queries from refetchQueries selection #8825

Merged
merged 2 commits into from
Sep 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A mock function call to assert how many times onQueryUpdated() is called would help us out in the future.

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing about mock functions.

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())
Comment on lines -770 to +772
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The include: "active" shorthand still excludes observerless queries, as before.

) {
return;
}

Expand Down