Skip to content

Commit 062ffe3

Browse files
authored
Ensure pollInterval is applied to useLazyQuery when it changes between renders (#13248)
Fixes #12832 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Fixed an issue where polling interval changes were not applied when the component rerendered, ensuring polling continues with the updated interval settings. <!-- review_stack_entry_start --> [![Review Change Stack](https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg)](https://app.coderabbit.ai/change-stack/apollographql/apollo-client/pull/13248?utm_source=github_walkthrough&utm_medium=github&utm_campaign=change_stack) <!-- review_stack_entry_end --> <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 5957d6f commit 062ffe3

3 files changed

Lines changed: 162 additions & 0 deletions

File tree

.changeset/honest-terms-eat.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@apollo/client": patch
3+
---
4+
5+
Fixes an issue where `useLazyQuery` would not apply a changed `pollInterval` between renders.
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
import {
2+
disableActEnvironment,
3+
renderHookToSnapshotStream,
4+
} from "@testing-library/react-render-stream";
5+
import React from "react";
6+
7+
import { gql, NetworkStatus } from "@apollo/client";
8+
import { useLazyQuery } from "@apollo/client/react";
9+
import { MockedProvider } from "@apollo/client/testing/react";
10+
11+
test("updates poll interval when rerendering with different pollInterval", async () => {
12+
const query = gql`
13+
query {
14+
hello
15+
}
16+
`;
17+
18+
let count = 0;
19+
20+
const wrapper = ({ children }: any) => (
21+
<MockedProvider
22+
mocks={[
23+
{
24+
request: { query },
25+
result: () => ({ data: { hello: `world ${++count}` } }),
26+
delay: 10,
27+
maxUsageCount: Number.POSITIVE_INFINITY,
28+
},
29+
]}
30+
>
31+
{children}
32+
</MockedProvider>
33+
);
34+
35+
using _disabledAct = disableActEnvironment();
36+
const renderStream = await renderHookToSnapshotStream(
37+
({ pollInterval }) => useLazyQuery(query, { pollInterval }),
38+
{ initialProps: { pollInterval: 50 }, wrapper }
39+
);
40+
41+
const { takeSnapshot, getCurrentSnapshot, rerender } = renderStream;
42+
43+
{
44+
const [, result] = await takeSnapshot();
45+
46+
expect(result).toStrictEqualTyped({
47+
data: undefined,
48+
dataState: "empty",
49+
called: false,
50+
loading: false,
51+
networkStatus: NetworkStatus.ready,
52+
previousData: undefined,
53+
variables: {},
54+
});
55+
}
56+
57+
const [execute] = getCurrentSnapshot();
58+
59+
await expect(execute()).resolves.toStrictEqualTyped({
60+
data: { hello: "world 1" },
61+
});
62+
63+
{
64+
const [, result] = await takeSnapshot();
65+
66+
expect(result).toStrictEqualTyped({
67+
data: undefined,
68+
dataState: "empty",
69+
called: true,
70+
loading: true,
71+
networkStatus: NetworkStatus.loading,
72+
previousData: undefined,
73+
variables: {},
74+
});
75+
}
76+
77+
{
78+
const [, result] = await takeSnapshot();
79+
80+
expect(result).toStrictEqualTyped({
81+
data: { hello: "world 1" },
82+
dataState: "complete",
83+
called: true,
84+
loading: false,
85+
networkStatus: NetworkStatus.ready,
86+
previousData: undefined,
87+
variables: {},
88+
});
89+
}
90+
91+
{
92+
const [, result] = await takeSnapshot({ timeout: 60 });
93+
94+
expect(result).toStrictEqualTyped({
95+
data: { hello: "world 1" },
96+
dataState: "complete",
97+
called: true,
98+
loading: true,
99+
networkStatus: NetworkStatus.poll,
100+
previousData: undefined,
101+
variables: {},
102+
});
103+
}
104+
105+
{
106+
const [, result] = await takeSnapshot();
107+
108+
expect(result).toStrictEqualTyped({
109+
data: { hello: "world 2" },
110+
dataState: "complete",
111+
called: true,
112+
loading: false,
113+
networkStatus: NetworkStatus.ready,
114+
previousData: { hello: "world 1" },
115+
variables: {},
116+
});
117+
}
118+
119+
await rerender({ pollInterval: 100 });
120+
121+
await expect(renderStream).toRerenderWithSimilarSnapshot();
122+
await expect(renderStream).not.toRerender({ timeout: 60 });
123+
124+
{
125+
const [, result] = await takeSnapshot();
126+
127+
expect(result).toStrictEqualTyped({
128+
data: { hello: "world 2" },
129+
dataState: "complete",
130+
called: true,
131+
loading: true,
132+
networkStatus: NetworkStatus.poll,
133+
previousData: { hello: "world 1" },
134+
variables: {},
135+
});
136+
}
137+
138+
{
139+
const [, result] = await takeSnapshot();
140+
141+
expect(result).toStrictEqualTyped({
142+
data: { hello: "world 3" },
143+
dataState: "complete",
144+
called: true,
145+
loading: false,
146+
networkStatus: NetworkStatus.ready,
147+
previousData: { hello: "world 2" },
148+
variables: {},
149+
});
150+
}
151+
152+
await rerender({ pollInterval: 0 });
153+
await expect(renderStream).toRerenderWithSimilarSnapshot();
154+
155+
await expect(renderStream).not.toRerender({ timeout: 150 });
156+
});

src/react/hooks/useLazyQuery.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,7 @@ export const useLazyQuery: useLazyQuery.Signature = function useLazyQuery<
565565
refetchWritePolicy: stableOptions?.refetchWritePolicy,
566566
returnPartialData: stableOptions?.returnPartialData,
567567
notifyOnNetworkStatusChange: stableOptions?.notifyOnNetworkStatusChange,
568+
pollInterval: stableOptions?.pollInterval,
568569
nextFetchPolicy: options?.nextFetchPolicy,
569570
skipPollAttempt: options?.skipPollAttempt,
570571
};

0 commit comments

Comments
 (0)