Skip to content

Commit a641c65

Browse files
committed
Fix regression for useUpdate and useUpdateMany
1 parent 32f71f6 commit a641c65

File tree

5 files changed

+52
-5
lines changed

5 files changed

+52
-5
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,21 @@ export const useCreate = <
158158
{ updatedAt }
159159
);
160160

161+
if (mutationMode === 'pessimistic') {
162+
queryClient.invalidateQueries({
163+
queryKey: [resource, 'getList'],
164+
});
165+
queryClient.invalidateQueries({
166+
queryKey: [resource, 'getInfiniteList'],
167+
});
168+
queryClient.invalidateQueries({
169+
queryKey: [resource, 'getMany'],
170+
});
171+
queryClient.invalidateQueries({
172+
queryKey: [resource, 'getManyReference'],
173+
});
174+
}
175+
161176
return clonedData;
162177
},
163178
getSnapshot: ({ resource, ...params }, { mutationMode }) => {

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,17 @@ export const useDelete = <
239239

240240
return snapshot;
241241
},
242+
onSettled: (
243+
result,
244+
error,
245+
variables,
246+
context: { snapshot: Snapshot }
247+
) => {
248+
// For deletion, we always refetch after error or success:
249+
context.snapshot.forEach(([queryKey]) => {
250+
queryClient.invalidateQueries({ queryKey });
251+
});
252+
},
242253
}
243254
);
244255

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,17 @@ export const useDeleteMany = <
266266
);
267267
return snapshot;
268268
},
269+
onSettled: (
270+
result,
271+
error,
272+
variables,
273+
context: { snapshot: Snapshot }
274+
) => {
275+
// For deletion, we always refetch after error or success:
276+
context.snapshot.forEach(([queryKey]) => {
277+
queryClient.invalidateQueries({ queryKey });
278+
});
279+
},
269280
}
270281
);
271282

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,15 @@ export const useMutationWithMutationMode = <
173173
variables = {},
174174
context: { snapshot: Snapshot }
175175
) => {
176-
// Always refetch after error or success:
177-
context.snapshot.forEach(([queryKey]) => {
178-
queryClient.invalidateQueries({ queryKey });
179-
});
176+
if (
177+
mode.current === 'optimistic' ||
178+
mode.current === 'undoable'
179+
) {
180+
// Always refetch after error or success:
181+
context.snapshot.forEach(([queryKey]) => {
182+
queryClient.invalidateQueries({ queryKey });
183+
});
184+
}
180185

181186
if (callTimeOnSettled.current) {
182187
return callTimeOnSettled.current(

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,12 @@ describe('useUpdateMany', () => {
542542
await waitFor(() => {
543543
expect(screen.queryByText('success')).not.toBeNull();
544544
expect(
545-
screen.queryByText('Hello World from middleware')
545+
// We could expect 'Hello World from middleware' here, but
546+
// updateMany's result only contains the ids, not the updated data
547+
// so the cache can only be updated with the call-time params,
548+
// which do not include the middleware's result.
549+
// I guess it's OK for most cases though...
550+
screen.queryByText('Hello World')
546551
).not.toBeNull();
547552
expect(screen.queryByText('mutating')).toBeNull();
548553
});

0 commit comments

Comments
 (0)