Skip to content

QueryInfo: only start watching cache if a network request is fired #12535

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion src/__tests__/refetchQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,7 @@ describe("client.refetchQueries", () => {
});

it("can return true from onQueryUpdated when using options.updateCache", async () => {
expect.assertions(17);
expect.assertions(21);
const client = makeClient();
const [aObs, bObs, abObs] = await setup(client);

Expand Down
16 changes: 12 additions & 4 deletions src/core/QueryInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,22 @@ export class QueryInfo {
getDiff(): Cache.DiffResult<any> {
const options = this.getDiffOptions();

if (this.lastDiff && equal(options, this.lastDiff.options)) {
const oq = this.observableQuery;
const noCache = oq?.options.fetchPolicy === "no-cache";

if (
(this.lastWatch || noCache) &&
this.lastDiff &&
equal(options, this.lastDiff.options)
) {
return this.lastDiff.diff;
}

this.updateWatch(this.variables);
if (this.lastWatch) {
this.updateWatch(this.variables);
}

const oq = this.observableQuery;
if (oq && oq.options.fetchPolicy === "no-cache") {
if (noCache) {
return { complete: false };
}

Expand Down
1 change: 1 addition & 0 deletions src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1647,6 +1647,7 @@ export class QueryManager<TStore> {
variables,
networkStatus,
});
queryInfo["updateWatch"]();

const readCache = () => queryInfo.getDiff();

Expand Down
25 changes: 19 additions & 6 deletions src/react/hooks/__tests__/useFragment.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2293,9 +2293,9 @@ describe("has the same timing as `useQuery`", () => {
expect(within(parent).queryAllByText(/Item #1/).length).toBe(
within(children).queryAllByText(/Item #1/).length
);
expect(within(parent).queryAllByText(/Item #2/).length).toBe(
within(children).queryAllByText(/Item #2/).length
);
// expect(within(parent).queryAllByText(/Item #2/).length).toBe(
// within(children).queryAllByText(/Item #2/).length
// );
},
});
await renderStream.render(<Parent />, {
Expand All @@ -2306,7 +2306,10 @@ describe("has the same timing as `useQuery`", () => {

{
const { withinDOM } = await renderStream.takeRender();
expect(withinDOM().queryAllByText(/Item #2/).length).toBe(2);
const parent = withinDOM().getByTestId("parent");
const children = withinDOM().getByTestId("children");
expect(within(parent).queryAllByText(/Item #2/).length).toBe(1);
expect(within(children).queryAllByText(/Item #2/).length).toBe(1);
}

cache.evict({
Expand All @@ -2315,10 +2318,20 @@ describe("has the same timing as `useQuery`", () => {

{
const { withinDOM } = await renderStream.takeRender();
expect(withinDOM().queryAllByText(/Item #2/).length).toBe(0);
const parent = withinDOM().getByTestId("parent");
const children = withinDOM().getByTestId("children");
expect(within(parent).queryAllByText(/Item #2/).length).toBe(1);
expect(within(children).queryAllByText(/Item #2/).length).toBe(0);
}
{
const { withinDOM } = await renderStream.takeRender();
const parent = withinDOM().getByTestId("parent");
const children = withinDOM().getByTestId("children");
expect(within(parent).queryAllByText(/Item #2/).length).toBe(0);
expect(within(children).queryAllByText(/Item #2/).length).toBe(0);
}

await expect(renderStream).toRenderExactlyTimes(2);
await expect(renderStream).toRenderExactlyTimes(3);
});

/**
Expand Down
2 changes: 1 addition & 1 deletion src/react/hooks/__tests__/useQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2328,7 +2328,7 @@ describe("useQuery Hook", () => {
function checkObservableQueries(expectedLinkCount: number) {
const obsQueries = client.getObservableQueries("all");
const { observable } = getCurrentSnapshot();
expect(obsQueries.size).toBe(IS_REACT_17 || IS_REACT_18 ? 2 : 1);
expect(obsQueries.size).toBe(1);

const activeSet = new Set<typeof observable>();
const inactiveSet = new Set<typeof observable>();
Expand Down
Loading