@@ -28,7 +28,7 @@ export const useMutationWithMutationMode = <
2828 mutationFn,
2929 getMutateWithMiddlewares,
3030 updateCache,
31- getSnapshot ,
31+ getQueryKeys ,
3232 onUndo,
3333 ...mutationOptions
3434 } = options ;
@@ -41,7 +41,28 @@ export const useMutationWithMutationMode = <
4141
4242 const mutationFnEvent = useEvent ( mutationFn ) ;
4343 const updateCacheEvent = useEvent ( updateCache ) ;
44- const getSnapshotEvent = useEvent ( getSnapshot ) ;
44+ const getQueryKeysEvent = useEvent ( getQueryKeys ) ;
45+ const getSnapshotEvent = useEvent (
46+ /**
47+ * Snapshot the previous values via queryClient.getQueriesData()
48+ *
49+ * The snapshotData ref will contain an array of tuples [query key, associated data]
50+ *
51+ * @example
52+ * [
53+ * [['posts', 'getList'], { data: [{ id: 1, title: 'Hello' }], total: 1 }],
54+ * [['posts', 'getMany'], [{ id: 1, title: 'Hello' }]],
55+ * ]
56+ *
57+ * @see https://tanstack.com/query/v5/docs/react/reference/QueryClient#queryclientgetqueriesdata
58+ */
59+ ( queryKeys : Array < QueryKey > ) =>
60+ queryKeys . reduce (
61+ ( prev , queryKey ) =>
62+ prev . concat ( queryClient . getQueriesData ( { queryKey } ) ) ,
63+ [ ] as Snapshot
64+ ) as Snapshot
65+ ) ;
4566 const onUndoEvent = useEvent ( onUndo ?? noop ) ;
4667 const getMutateWithMiddlewaresEvent = useEvent (
4768 getMutateWithMiddlewares ??
@@ -171,9 +192,17 @@ export const useMutationWithMutationMode = <
171192 mode . current === 'optimistic' ||
172193 mode . current === 'undoable'
173194 ) {
174- const [ , , , onMutateResult ] = args ;
195+ const [ , , variables , onMutateResult ] = args ;
175196
176197 // Always refetch after error or success:
198+ getQueryKeysEvent (
199+ { ...paramsRef . current , ...variables } ,
200+ {
201+ mutationMode : mode . current ,
202+ }
203+ ) . forEach ( queryKey => {
204+ queryClient . invalidateQueries ( { queryKey } ) ;
205+ } ) ;
177206 ( onMutateResult as { snapshot : Snapshot } ) . snapshot . forEach (
178207 ( [ queryKey ] ) => {
179208 queryClient . invalidateQueries ( { queryKey } ) ;
@@ -243,10 +272,12 @@ export const useMutationWithMutationMode = <
243272 }
244273
245274 snapshot . current = getSnapshotEvent (
246- { ...paramsRef . current , ...callTimeParams } ,
247- {
248- mutationMode : mode . current ,
249- }
275+ getQueryKeysEvent (
276+ { ...paramsRef . current , ...callTimeParams } ,
277+ {
278+ mutationMode : mode . current ,
279+ }
280+ )
250281 ) ;
251282
252283 if ( mode . current === 'pessimistic' ) {
@@ -390,10 +421,10 @@ export type UseMutationWithMutationModeOptions<
390421 options : OptionsType ,
391422 mutationResult : TData [ 'data' ] | undefined
392423 ) => TData [ 'data' ] ;
393- getSnapshot : < OptionsType extends { mutationMode : MutationMode } > (
424+ getQueryKeys : < OptionsType extends { mutationMode : MutationMode } > (
394425 params : Partial < TVariables > ,
395426 options : OptionsType
396- ) => Snapshot ;
427+ ) => Array < QueryKey > ;
397428 onUndo ?: < OptionsType extends { mutationMode : MutationMode } > (
398429 params : Partial < TVariables > ,
399430 options : OptionsType
0 commit comments