Skip to content

Commit d2a60d4

Browse files
authored
only advance previousData if data changed (#12637)
1 parent b147cac commit d2a60d4

File tree

4 files changed

+46
-75
lines changed

4 files changed

+46
-75
lines changed

.changeset/slimy-ants-bake.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@apollo/client": major
3+
---
4+
5+
`useQuery`: only advance `previousData` if `data` actually changed

src/react/hooks/__tests__/useMutation.test.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2989,7 +2989,7 @@ describe("useMutation Hook", () => {
29892989
data: GET_TODOS_RESULT_1,
29902990
loading: true,
29912991
networkStatus: NetworkStatus.refetch,
2992-
previousData: GET_TODOS_RESULT_1,
2992+
previousData: undefined,
29932993
variables: {},
29942994
});
29952995

@@ -3011,7 +3011,7 @@ describe("useMutation Hook", () => {
30113011
data: GET_TODOS_RESULT_1,
30123012
loading: true,
30133013
networkStatus: NetworkStatus.refetch,
3014-
previousData: GET_TODOS_RESULT_1,
3014+
previousData: undefined,
30153015
variables: {},
30163016
});
30173017

@@ -3188,7 +3188,7 @@ describe("useMutation Hook", () => {
31883188
data: GET_TODOS_RESULT_1,
31893189
loading: true,
31903190
networkStatus: NetworkStatus.refetch,
3191-
previousData: GET_TODOS_RESULT_1,
3191+
previousData: undefined,
31923192
variables: {},
31933193
});
31943194

@@ -3210,7 +3210,7 @@ describe("useMutation Hook", () => {
32103210
data: GET_TODOS_RESULT_1,
32113211
loading: true,
32123212
networkStatus: NetworkStatus.refetch,
3213-
previousData: GET_TODOS_RESULT_1,
3213+
previousData: undefined,
32143214
variables: {},
32153215
});
32163216

src/react/hooks/__tests__/useQuery.test.tsx

Lines changed: 32 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1780,7 +1780,7 @@ describe("useQuery Hook", () => {
17801780
data: { linkCount: 1 },
17811781
loading: true,
17821782
networkStatus: NetworkStatus.loading,
1783-
previousData: { linkCount: 1 },
1783+
previousData: undefined,
17841784
variables: {},
17851785
});
17861786
}
@@ -1870,7 +1870,7 @@ describe("useQuery Hook", () => {
18701870
data: { hello: "world 1" },
18711871
loading: true,
18721872
networkStatus: NetworkStatus.poll,
1873-
previousData: { hello: "world 1" },
1873+
previousData: undefined,
18741874
variables: {},
18751875
});
18761876
}
@@ -1894,7 +1894,7 @@ describe("useQuery Hook", () => {
18941894
data: { hello: "world 2" },
18951895
loading: true,
18961896
networkStatus: NetworkStatus.poll,
1897-
previousData: { hello: "world 2" },
1897+
previousData: { hello: "world 1" },
18981898
variables: {},
18991899
});
19001900
}
@@ -2003,7 +2003,7 @@ describe("useQuery Hook", () => {
20032003
data: { hello: "world 1" },
20042004
loading: true,
20052005
networkStatus: NetworkStatus.poll,
2006-
previousData: { hello: "world 1" },
2006+
previousData: undefined,
20072007
variables: {},
20082008
});
20092009
}
@@ -2027,7 +2027,7 @@ describe("useQuery Hook", () => {
20272027
data: { hello: "world 2" },
20282028
loading: true,
20292029
networkStatus: NetworkStatus.poll,
2030-
previousData: { hello: "world 2" },
2030+
previousData: { hello: "world 1" },
20312031
variables: {},
20322032
});
20332033
}
@@ -2267,7 +2267,7 @@ describe("useQuery Hook", () => {
22672267
data: { hello: "world 1" },
22682268
loading: true,
22692269
networkStatus: NetworkStatus.poll,
2270-
previousData: { hello: "world 1" },
2270+
previousData: undefined,
22712271
variables: {},
22722272
});
22732273
await expect(takeSnapshot()).resolves.toStrictEqualTyped({
@@ -2450,7 +2450,7 @@ describe("useQuery Hook", () => {
24502450
data: { hello: "world 1" },
24512451
loading: true,
24522452
networkStatus: NetworkStatus.poll,
2453-
previousData: { hello: "world 1" },
2453+
previousData: undefined,
24542454
variables: {},
24552455
});
24562456
expect(requestSpy).toHaveBeenCalledTimes(2);
@@ -2550,7 +2550,7 @@ describe("useQuery Hook", () => {
25502550
data: { hello: "world 1" },
25512551
loading: true,
25522552
networkStatus: NetworkStatus.poll,
2553-
previousData: { hello: "world 1" },
2553+
previousData: undefined,
25542554
variables: {},
25552555
});
25562556
}
@@ -2582,7 +2582,7 @@ describe("useQuery Hook", () => {
25822582
data: { hello: "world 2" },
25832583
loading: true,
25842584
networkStatus: NetworkStatus.poll,
2585-
previousData: { hello: "world 2" },
2585+
previousData: { hello: "world 1" },
25862586
variables: {},
25872587
});
25882588
}
@@ -2606,7 +2606,7 @@ describe("useQuery Hook", () => {
26062606
data: { hello: "world 3" },
26072607
loading: true,
26082608
networkStatus: NetworkStatus.poll,
2609-
previousData: { hello: "world 3" },
2609+
previousData: { hello: "world 2" },
26102610
variables: {},
26112611
});
26122612
}
@@ -4062,7 +4062,7 @@ describe("useQuery Hook", () => {
40624062
data: { hello: "world" },
40634063
loading: true,
40644064
networkStatus: NetworkStatus.refetch,
4065-
previousData: { hello: "world" },
4065+
previousData: undefined,
40664066
variables: {},
40674067
});
40684068
}
@@ -4077,7 +4077,7 @@ describe("useQuery Hook", () => {
40774077
}),
40784078
loading: false,
40794079
networkStatus: NetworkStatus.error,
4080-
previousData: { hello: "world" },
4080+
previousData: undefined,
40814081
variables: {},
40824082
});
40834083
}
@@ -4180,7 +4180,7 @@ describe("useQuery Hook", () => {
41804180
data: { letters: ab },
41814181
loading: true,
41824182
networkStatus: NetworkStatus.fetchMore,
4183-
previousData: { letters: ab },
4183+
previousData: undefined,
41844184
variables: { limit: 2 },
41854185
});
41864186
}
@@ -4258,7 +4258,7 @@ describe("useQuery Hook", () => {
42584258
data: { letters: ab },
42594259
loading: true,
42604260
networkStatus: NetworkStatus.fetchMore,
4261-
previousData: { letters: ab },
4261+
previousData: undefined,
42624262
variables: { limit: 2 },
42634263
});
42644264
}
@@ -4341,7 +4341,7 @@ describe("useQuery Hook", () => {
43414341
data: { letters: ab },
43424342
loading: true,
43434343
networkStatus: NetworkStatus.fetchMore,
4344-
previousData: { letters: ab },
4344+
previousData: undefined,
43454345
variables: { limit: 2 },
43464346
});
43474347
}
@@ -4428,7 +4428,7 @@ describe("useQuery Hook", () => {
44284428
data: { letters: ab },
44294429
loading: true,
44304430
networkStatus: NetworkStatus.fetchMore,
4431-
previousData: { letters: ab },
4431+
previousData: undefined,
44324432
variables: { limit: 2 },
44334433
});
44344434
}
@@ -4574,12 +4574,7 @@ describe("useQuery Hook", () => {
45744574
},
45754575
loading: true,
45764576
networkStatus: NetworkStatus.fetchMore,
4577-
previousData: {
4578-
letters: [
4579-
{ __typename: "Letter", letter: "A", position: 1 },
4580-
{ __typename: "Letter", letter: "B", position: 2 },
4581-
],
4582-
},
4577+
previousData: undefined,
45834578
variables: { limit: 2 },
45844579
});
45854580
}
@@ -4652,8 +4647,6 @@ describe("useQuery Hook", () => {
46524647
letters: [
46534648
{ __typename: "Letter", letter: "A", position: 1 },
46544649
{ __typename: "Letter", letter: "B", position: 2 },
4655-
{ __typename: "Letter", letter: "C", position: 3 },
4656-
{ __typename: "Letter", letter: "D", position: 4 },
46574650
],
46584651
},
46594652
variables: { limit: 2 },
@@ -5942,18 +5935,7 @@ describe("useQuery Hook", () => {
59425935
},
59435936
loading: true,
59445937
networkStatus: NetworkStatus.loading,
5945-
previousData: {
5946-
author: {
5947-
__typename: "Author",
5948-
id: 1,
5949-
name: "Author Lee",
5950-
post: {
5951-
__typename: "Post",
5952-
id: 1,
5953-
title: "Title",
5954-
},
5955-
},
5956-
},
5938+
previousData: undefined,
59575939
variables: {},
59585940
});
59595941
}
@@ -6155,7 +6137,7 @@ describe("useQuery Hook", () => {
61556137
data: { hello: "world 1" },
61566138
loading: true,
61576139
networkStatus: NetworkStatus.refetch,
6158-
previousData: { hello: "world 1" },
6140+
previousData: undefined,
61596141
variables: {},
61606142
});
61616143
}
@@ -6168,7 +6150,7 @@ describe("useQuery Hook", () => {
61686150
error: new Error("This is an error!"),
61696151
loading: false,
61706152
networkStatus: NetworkStatus.error,
6171-
previousData: { hello: "world 1" },
6153+
previousData: undefined,
61726154
variables: {},
61736155
});
61746156
}
@@ -6184,7 +6166,7 @@ describe("useQuery Hook", () => {
61846166
data: { hello: "world 1" },
61856167
loading: true,
61866168
networkStatus: NetworkStatus.refetch,
6187-
previousData: { hello: "world 1" },
6169+
previousData: undefined,
61886170
variables: {},
61896171
});
61906172
}
@@ -6314,7 +6296,7 @@ describe("useQuery Hook", () => {
63146296
// This networkStatus is setVariables instead of refetch because we
63156297
// called refetch with new variables.
63166298
networkStatus: NetworkStatus.refetch,
6317-
previousData: { primes: [2, 3, 5, 7, 11] },
6299+
previousData: undefined,
63186300
variables: { min: 12, max: 30 },
63196301
});
63206302
}
@@ -6417,7 +6399,7 @@ describe("useQuery Hook", () => {
64176399
// This networkStatus is setVariables instead of refetch because we
64186400
// called refetch with new variables.
64196401
networkStatus: NetworkStatus.refetch,
6420-
previousData: { primes: [2, 3, 5, 7, 11] },
6402+
previousData: undefined,
64216403
variables: { min: 12, max: 30 },
64226404
});
64236405
}
@@ -6520,7 +6502,7 @@ describe("useQuery Hook", () => {
65206502
// This networkStatus is setVariables instead of refetch because we
65216503
// called refetch with new variables.
65226504
networkStatus: NetworkStatus.refetch,
6523-
previousData: { primes: [2, 3, 5, 7, 11] },
6505+
previousData: undefined,
65246506
variables: { min: 12, max: 30 },
65256507
});
65266508
}
@@ -6667,13 +6649,7 @@ describe("useQuery Hook", () => {
66676649
},
66686650
loading: true,
66696651
networkStatus: NetworkStatus.refetch,
6670-
previousData: {
6671-
car: {
6672-
__typename: "Car",
6673-
make: "Audi",
6674-
model: "A4",
6675-
},
6676-
},
6652+
previousData: undefined,
66776653
variables: { id: 1 },
66786654
});
66796655
}
@@ -7463,7 +7439,7 @@ describe("useQuery Hook", () => {
74637439
data: { hello: 1 },
74647440
loading: true,
74657441
networkStatus: NetworkStatus.setVariables,
7466-
previousData: { hello: 1 },
7442+
previousData: undefined,
74677443
variables: { newVar: true },
74687444
});
74697445

@@ -8032,7 +8008,7 @@ describe("useQuery Hook", () => {
80328008
data: data1,
80338009
loading: true,
80348010
networkStatus: NetworkStatus.refetch,
8035-
previousData: data1,
8011+
previousData: undefined,
80368012
variables: {},
80378013
});
80388014

@@ -8137,7 +8113,7 @@ describe("useQuery Hook", () => {
81378113
data: data1,
81388114
loading: true,
81398115
networkStatus: NetworkStatus.refetch,
8140-
previousData: data1,
8116+
previousData: undefined,
81418117
variables: {},
81428118
});
81438119

@@ -8296,7 +8272,7 @@ describe("useQuery Hook", () => {
82968272
data: { a: "aa", b: 1 },
82978273
loading: true,
82988274
networkStatus: NetworkStatus.loading,
8299-
previousData: { a: "aa", b: 1 },
8275+
previousData: { a: "a" },
83008276
variables: {},
83018277
});
83028278

@@ -8509,7 +8485,7 @@ describe("useQuery Hook", () => {
85098485
data: { hello: "world 1" },
85108486
loading: true,
85118487
networkStatus: NetworkStatus.poll,
8512-
previousData: { hello: "world 1" },
8488+
previousData: undefined,
85138489
variables: {},
85148490
});
85158491

@@ -8525,7 +8501,7 @@ describe("useQuery Hook", () => {
85258501
data: { hello: "world 2" },
85268502
loading: true,
85278503
networkStatus: NetworkStatus.poll,
8528-
previousData: { hello: "world 2" },
8504+
previousData: { hello: "world 1" },
85298505
variables: {},
85308506
});
85318507

@@ -9555,21 +9531,7 @@ describe("useQuery Hook", () => {
95559531
}),
95569532
loading: false,
95579533
networkStatus: NetworkStatus.error,
9558-
previousData: {
9559-
hero: {
9560-
heroFriends: [
9561-
{
9562-
id: "1000",
9563-
name: "Luke Skywalker",
9564-
},
9565-
{
9566-
id: "1003",
9567-
name: "Leia Organa",
9568-
},
9569-
],
9570-
name: "R2-D2",
9571-
},
9572-
},
9534+
previousData: undefined,
95739535
variables: {},
95749536
});
95759537

src/react/hooks/useQuery.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,11 @@ function useResultSubscription<TData, TVariables extends OperationVariables>(
391391
return;
392392
}
393393

394-
if (previousResult && previousResult.data) {
394+
if (
395+
previousResult &&
396+
previousResult.data &&
397+
!equal(previousResult.data, result.data)
398+
) {
395399
// eslint-disable-next-line react-compiler/react-compiler
396400
resultData.previousData = previousResult.data;
397401
}

0 commit comments

Comments
 (0)