Skip to content

Commit 827f064

Browse files
authored
Merge pull request #10964 from marmelab/react-query-bc
Fix incompatibility with latest react-query
2 parents d6f9c80 + 32e4049 commit 827f064

30 files changed

+291
-262
lines changed

examples/simple/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
"dependencies": {
1313
"@mui/icons-material": "^5.16.12",
1414
"@mui/material": "^5.16.12",
15-
"@tanstack/react-query": "^5.83.0",
16-
"@tanstack/react-query-devtools": "^5.83.0",
15+
"@tanstack/react-query": "^5.90.2",
16+
"@tanstack/react-query-devtools": "^5.90.2",
1717
"fakerest": "^4.1.3",
1818
"jsonexport": "^3.2.0",
1919
"lodash": "~4.17.5",

packages/ra-core/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
},
2929
"devDependencies": {
3030
"@hookform/resolvers": "^3.2.0",
31-
"@tanstack/react-query-devtools": "^5.21.7",
31+
"@tanstack/react-query": "^5.90.2",
32+
"@tanstack/react-query-devtools": "^5.90.2",
3233
"@testing-library/react": "^15.0.7",
3334
"@types/jest": "^29.5.2",
3435
"@types/jscodeshift": "^0.11.11",

packages/ra-core/src/controller/create/CreateBase.spec.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ describe('CreateBase', () => {
5555
test: 'test',
5656
},
5757
{ data: { test: 'test' }, resource: 'posts' },
58-
{ snapshot: [] }
58+
{ snapshot: [] },
59+
expect.anything()
5960
);
6061
});
6162
});
@@ -87,7 +88,8 @@ describe('CreateBase', () => {
8788
test: 'test',
8889
},
8990
{ data: { test: 'test' }, resource: 'posts' },
90-
{ snapshot: [] }
91+
{ snapshot: [] },
92+
expect.anything()
9193
);
9294
});
9395
expect(onSuccess).not.toHaveBeenCalled();
@@ -114,7 +116,8 @@ describe('CreateBase', () => {
114116
expect(onError).toHaveBeenCalledWith(
115117
{ message: 'test' },
116118
{ data: { test: 'test' }, resource: 'posts' },
117-
{ snapshot: [] }
119+
{ snapshot: [] },
120+
expect.anything()
118121
);
119122
});
120123
});
@@ -141,7 +144,8 @@ describe('CreateBase', () => {
141144
expect(onErrorOverride).toHaveBeenCalledWith(
142145
{ message: 'test' },
143146
{ data: { test: 'test' }, resource: 'posts' },
144-
{ snapshot: [] }
147+
expect.anything(),
148+
expect.anything()
145149
);
146150
});
147151
expect(onError).not.toHaveBeenCalled();

packages/ra-core/src/controller/create/useCreateController.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,11 @@ export const useCreateController = <
9292
MutationOptionsError,
9393
ResultRecordType
9494
>(resource, undefined, {
95-
onSuccess: async (data, variables, context) => {
95+
onSuccess: async (...args) => {
9696
if (onSuccess) {
97-
return onSuccess(data, variables, context);
97+
return onSuccess(...args);
9898
}
99+
const [data] = args;
99100
notify(`resources.${resource}.notifications.created`, {
100101
type: 'info',
101102
messageArgs: {
@@ -108,10 +109,11 @@ export const useCreateController = <
108109
});
109110
redirect(finalRedirectTo, resource, data.id, data);
110111
},
111-
onError: (error: MutationOptionsError, variables, context) => {
112+
onError: (...args) => {
112113
if (onError) {
113-
return onError(error, variables, context);
114+
return onError(...args);
114115
}
116+
const [error] = args;
115117
// Don't trigger a notification if this is a validation error
116118
// (notification will be handled by the useNotifyIsFormInvalid hook)
117119
const validationErrors = (error as HttpError)?.body?.errors;

packages/ra-core/src/controller/edit/EditBase.spec.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ describe('EditBase', () => {
8181
resource: 'posts',
8282
meta: undefined,
8383
},
84-
{ snapshot: [] }
84+
{ snapshot: [] },
85+
expect.anything()
8586
);
8687
});
8788
});
@@ -125,7 +126,8 @@ describe('EditBase', () => {
125126
resource: 'posts',
126127
meta: undefined,
127128
},
128-
{ snapshot: [] }
129+
{ snapshot: [] },
130+
expect.anything()
129131
);
130132
});
131133
expect(onSuccess).not.toHaveBeenCalled();
@@ -162,7 +164,8 @@ describe('EditBase', () => {
162164
resource: 'posts',
163165
meta: undefined,
164166
},
165-
{ snapshot: [] }
167+
{ snapshot: [] },
168+
expect.anything()
166169
);
167170
});
168171
});
@@ -199,7 +202,8 @@ describe('EditBase', () => {
199202
resource: 'posts',
200203
meta: undefined,
201204
},
202-
{ snapshot: [] }
205+
{ snapshot: [] },
206+
expect.anything()
203207
);
204208
});
205209
expect(onError).not.toHaveBeenCalled();

packages/ra-core/src/controller/edit/useEditController.spec.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,8 @@ describe('useEditController', () => {
459459
resource: 'posts',
460460
meta: undefined,
461461
},
462-
{ snapshot: expect.any(Array) }
462+
{ snapshot: expect.any(Array) },
463+
expect.anything()
463464
)
464465
);
465466
expect(notificationsSpy).toEqual([]);
@@ -513,7 +514,8 @@ describe('useEditController', () => {
513514
resource: 'posts',
514515
meta: undefined,
515516
},
516-
{ snapshot: expect.any(Array) }
517+
{ snapshot: expect.any(Array) },
518+
expect.anything()
517519
)
518520
);
519521
expect(notificationsSpy).toEqual([]);
@@ -566,7 +568,8 @@ describe('useEditController', () => {
566568
resource: 'posts',
567569
meta: undefined,
568570
},
569-
{ snapshot: expect.any(Array) }
571+
{ snapshot: expect.any(Array) },
572+
expect.anything()
570573
)
571574
);
572575
expect(notificationsSpy).toEqual([]);

packages/ra-core/src/controller/edit/useEditController.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,11 @@ export const useEditController = <
175175
resource,
176176
recordCached,
177177
{
178-
onSuccess: async (data, variables, context) => {
178+
onSuccess: async (...args) => {
179179
if (onSuccess) {
180-
return onSuccess(data, variables, context);
180+
return onSuccess(...args);
181181
}
182+
const [data] = args;
182183
notify(`resources.${resource}.notifications.updated`, {
183184
type: 'info',
184185
messageArgs: {
@@ -191,10 +192,11 @@ export const useEditController = <
191192
});
192193
redirect(redirectTo, resource, data.id, data);
193194
},
194-
onError: (error, variables, context) => {
195+
onError: (...args) => {
195196
if (onError) {
196-
return onError(error, variables, context);
197+
return onError(...args);
197198
}
199+
const [error] = args;
198200
// Don't trigger a notification if this is a validation error
199201
// (notification will be handled by the useNotifyIsFormInvalid hook)
200202
const validationErrors = (error as HttpError)?.body?.errors;

packages/ra-core/src/controller/list/useListController.spec.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,6 @@ describe('useListController', () => {
240240
);
241241

242242
// Check that the permanent filter is not included in the displayedFilters and filterValues (passed to Filter form and button)
243-
expect(children).toHaveBeenCalledTimes(2);
244243
expect(children).toHaveBeenCalledWith(
245244
expect.objectContaining({
246245
displayedFilters: {},
@@ -264,7 +263,7 @@ describe('useListController', () => {
264263
'posts',
265264
expect.objectContaining({ filter: { foo: 2 } })
266265
);
267-
expect(children).toHaveBeenCalledTimes(4);
266+
expect(children).toHaveBeenCalledTimes(3);
268267
});
269268
});
270269

packages/ra-core/src/dataProvider/useCreate.ts

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,10 @@ export const useCreate = <
184184
.then(({ data }) => data);
185185
},
186186
...mutationOptions,
187-
onMutate: async (
188-
variables: Partial<UseCreateMutateParams<RecordType>>
189-
) => {
187+
onMutate: async (...args) => {
190188
if (mutationOptions.onMutate) {
191189
const userContext =
192-
(await mutationOptions.onMutate(variables)) || {};
190+
(await mutationOptions.onMutate(...args)) || {};
193191
return {
194192
snapshot: snapshot.current,
195193
// @ts-ignore
@@ -200,27 +198,27 @@ export const useCreate = <
200198
return { snapshot: snapshot.current };
201199
}
202200
},
203-
onError: (error, variables, context: { snapshot: Snapshot }) => {
201+
onError: (...args) => {
204202
if (mode.current === 'optimistic' || mode.current === 'undoable') {
203+
const [, , onMutateResult] = args;
205204
// If the mutation fails, use the context returned from onMutate to rollback
206-
context.snapshot.forEach(([key, value]) => {
207-
queryClient.setQueryData(key, value);
208-
});
205+
(onMutateResult as { snapshot: Snapshot }).snapshot.forEach(
206+
([key, value]) => {
207+
queryClient.setQueryData(key, value);
208+
}
209+
);
209210
}
210211
if (callTimeOnError.current) {
211-
return callTimeOnError.current(error, variables, context);
212+
return callTimeOnError.current(...args);
212213
}
213214
if (mutationOptions.onError) {
214-
return mutationOptions.onError(error, variables, context);
215+
return mutationOptions.onError(...args);
215216
}
216217
// call-time error callback is executed by react-query
217218
},
218-
onSuccess: (
219-
data: ResultRecordType,
220-
variables: Partial<UseCreateMutateParams<RecordType>> = {},
221-
context: unknown
222-
) => {
219+
onSuccess: (...args) => {
223220
if (mode.current === 'pessimistic') {
221+
const [data, variables] = args;
224222
const { resource: callTimeResource = resource } = variables;
225223
queryClient.setQueryData(
226224
[callTimeResource, 'getOne', { id: String(data.id) }],
@@ -243,38 +241,26 @@ export const useCreate = <
243241
mutationOptions.onSuccess &&
244242
!hasCallTimeOnSuccess.current
245243
) {
246-
mutationOptions.onSuccess(data, variables, context);
244+
mutationOptions.onSuccess(...args);
247245
}
248246
}
249247
},
250-
onSettled: (
251-
data,
252-
error,
253-
variables,
254-
context: { snapshot: Snapshot }
255-
) => {
248+
onSettled: (...args) => {
256249
if (mode.current === 'optimistic' || mode.current === 'undoable') {
250+
const [, , , onMutateResult] = args;
257251
// Always refetch after error or success:
258-
context.snapshot.forEach(([queryKey]) => {
259-
queryClient.invalidateQueries({ queryKey });
260-
});
252+
(onMutateResult as { snapshot: Snapshot }).snapshot.forEach(
253+
([queryKey]) => {
254+
queryClient.invalidateQueries({ queryKey });
255+
}
256+
);
261257
}
262258

263259
if (callTimeOnSettled.current) {
264-
return callTimeOnSettled.current(
265-
data,
266-
error,
267-
variables,
268-
context
269-
);
260+
return callTimeOnSettled.current(...args);
270261
}
271262
if (mutationOptions.onSettled) {
272-
return mutationOptions.onSettled(
273-
data,
274-
error,
275-
variables,
276-
context
277-
);
263+
return mutationOptions.onSettled(...args);
278264
}
279265
},
280266
});
@@ -409,7 +395,12 @@ export const useCreate = <
409395
onSuccess(
410396
callTimeData as unknown as ResultRecordType,
411397
{ resource: callTimeResource, ...callTimeParams },
412-
{ snapshot: snapshot.current }
398+
{ snapshot: snapshot.current },
399+
{
400+
client: queryClient,
401+
mutationKey: [resource, 'create', params],
402+
meta: mutationOptions.meta,
403+
}
413404
);
414405
} else if (
415406
mutationOptions.onSuccess &&
@@ -418,7 +409,12 @@ export const useCreate = <
418409
mutationOptions.onSuccess(
419410
callTimeData as unknown as ResultRecordType,
420411
{ resource: callTimeResource, ...callTimeParams },
421-
{ snapshot: snapshot.current }
412+
{ snapshot: snapshot.current },
413+
{
414+
client: queryClient,
415+
mutationKey: [resource, 'create', params],
416+
meta: mutationOptions.meta,
417+
}
422418
);
423419
}
424420
}, 0);

packages/ra-core/src/dataProvider/useDelete.spec.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,8 @@ describe('useDelete', () => {
336336
expect(onSuccess).toHaveBeenCalledWith(
337337
{ id: 1 },
338338
{ id: 1, previousData: { foo: 456 }, resource: 'foo' },
339-
{ snapshot: [] }
339+
{ snapshot: [] },
340+
expect.anything()
340341
);
341342
});
342343
});
@@ -372,7 +373,8 @@ describe('useDelete', () => {
372373
expect(onError).toHaveBeenCalledWith(
373374
new Error('not good'),
374375
{ id: 1, previousData: { foo: 456 }, resource: 'foo' },
375-
{ snapshot: [] }
376+
{ snapshot: [] },
377+
expect.anything()
376378
);
377379
});
378380
});

0 commit comments

Comments
 (0)