Skip to content

Commit 035cda8

Browse files
committed
Fix incompatibility with latest react-query
1 parent 11e2c04 commit 035cda8

File tree

15 files changed

+230
-238
lines changed

15 files changed

+230
-238
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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
},
2929
"devDependencies": {
3030
"@hookform/resolvers": "^3.2.0",
31-
"@tanstack/react-query-devtools": "^5.21.7",
31+
"@tanstack/react-query-devtools": "^5.90.2",
3232
"@testing-library/react": "^15.0.7",
3333
"@types/jest": "^29.5.2",
3434
"@types/jscodeshift": "^0.11.11",
@@ -58,7 +58,7 @@
5858
"react-router-dom": "^6.28.1 || ^7.1.1"
5959
},
6060
"dependencies": {
61-
"@tanstack/react-query": "^5.83.0",
61+
"@tanstack/react-query": "^5.90.2",
6262
"date-fns": "^3.6.0",
6363
"eventemitter3": "^5.0.1",
6464
"inflection": "^3.0.0",

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/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/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.ts

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,10 @@ export const useDelete = <
227227
.then(({ data }) => data);
228228
},
229229
...mutationOptions,
230-
onMutate: async (
231-
variables: Partial<UseDeleteMutateParams<RecordType>>
232-
) => {
230+
onMutate: async (...args) => {
233231
if (mutationOptions.onMutate) {
234232
const userContext =
235-
(await mutationOptions.onMutate(variables)) || {};
233+
(await mutationOptions.onMutate(...args)) || {};
236234
return {
237235
snapshot: snapshot.current,
238236
// @ts-ignore
@@ -243,29 +241,25 @@ export const useDelete = <
243241
return { snapshot: snapshot.current };
244242
}
245243
},
246-
onError: (
247-
error: MutationError,
248-
variables: Partial<UseDeleteMutateParams<RecordType>> = {},
249-
context: { snapshot: Snapshot }
250-
) => {
244+
onError: (...args) => {
251245
if (mode.current === 'optimistic' || mode.current === 'undoable') {
246+
const [, , onMutateResult] = args;
252247
// If the mutation fails, use the context returned from onMutate to rollback
253-
context.snapshot.forEach(([key, value]) => {
254-
queryClient.setQueryData(key, value);
255-
});
248+
(onMutateResult as { snapshot: Snapshot }).snapshot.forEach(
249+
([key, value]) => {
250+
queryClient.setQueryData(key, value);
251+
}
252+
);
256253
}
257254

258255
if (mutationOptions.onError && !hasCallTimeOnError.current) {
259-
return mutationOptions.onError(error, variables, context);
256+
return mutationOptions.onError(...args);
260257
}
261258
// call-time error callback is executed by react-query
262259
},
263-
onSuccess: (
264-
data: RecordType,
265-
variables: Partial<UseDeleteMutateParams<RecordType>> = {},
266-
context: unknown
267-
) => {
260+
onSuccess: (...args) => {
268261
if (mode.current === 'pessimistic') {
262+
const [, variables] = args;
269263
// update the getOne and getList query cache with the new result
270264
const {
271265
resource: callTimeResource = resource,
@@ -280,29 +274,23 @@ export const useDelete = <
280274
mutationOptions.onSuccess &&
281275
!hasCallTimeOnSuccess.current
282276
) {
283-
mutationOptions.onSuccess(data, variables, context);
277+
mutationOptions.onSuccess(...args);
284278
}
285279
// call-time success callback is executed by react-query
286280
}
287281
},
288-
onSettled: (
289-
data: RecordType,
290-
error: MutationError,
291-
variables: Partial<UseDeleteMutateParams<RecordType>> = {},
292-
context: { snapshot: Snapshot }
293-
) => {
282+
onSettled: (...args) => {
283+
const [, , , onMutateResult] = args;
284+
294285
// Always refetch after error or success:
295-
context.snapshot.forEach(([queryKey]) => {
296-
queryClient.invalidateQueries({ queryKey });
297-
});
286+
(onMutateResult as { snapshot: Snapshot }).snapshot.forEach(
287+
([queryKey]) => {
288+
queryClient.invalidateQueries({ queryKey });
289+
}
290+
);
298291

299292
if (mutationOptions.onSettled && !hasCallTimeOnSettled.current) {
300-
return mutationOptions.onSettled(
301-
data,
302-
error,
303-
variables,
304-
context
305-
);
293+
return mutationOptions.onSettled(...args);
306294
}
307295
},
308296
});
@@ -397,15 +385,25 @@ export const useDelete = <
397385
setTimeout(() => {
398386
if (callTimeOptions.onSuccess) {
399387
callTimeOptions.onSuccess(
400-
callTimePreviousData,
388+
callTimePreviousData as RecordType,
401389
{ resource: callTimeResource, ...callTimeParams },
402-
{ snapshot: snapshot.current }
390+
{ snapshot: snapshot.current },
391+
{
392+
client: queryClient,
393+
mutationKey: [resource, 'delete', params],
394+
meta: mutationOptions.meta,
395+
}
403396
);
404397
} else if (mutationOptions.onSuccess) {
405398
mutationOptions.onSuccess(
406-
callTimePreviousData,
399+
callTimePreviousData as RecordType,
407400
{ resource: callTimeResource, ...callTimeParams },
408-
{ snapshot: snapshot.current }
401+
{ snapshot: snapshot.current },
402+
{
403+
client: queryClient,
404+
mutationKey: [resource, 'delete', params],
405+
meta: mutationOptions.meta,
406+
}
409407
);
410408
}
411409
}, 0);

0 commit comments

Comments
 (0)