Repository navigation
Fix incompatibility with latest react-query #10964
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -184,12 +184,10 @@ export const useCreate = < | |
| .then(({ data }) => data); | ||
| }, | ||
| ...mutationOptions, | ||
| onMutate: async ( | ||
| variables: Partial<UseCreateMutateParams<RecordType>> | ||
| ) => { | ||
| onMutate: async (...args) => { | ||
| if (mutationOptions.onMutate) { | ||
| const userContext = | ||
| (await mutationOptions.onMutate(variables)) || {}; | ||
| (await mutationOptions.onMutate(...args)) || {}; | ||
| return { | ||
| snapshot: snapshot.current, | ||
| // @ts-ignore | ||
|
|
@@ -200,27 +198,27 @@ export const useCreate = < | |
| return { snapshot: snapshot.current }; | ||
| } | ||
| }, | ||
| onError: (error, variables, context: { snapshot: Snapshot }) => { | ||
| onError: (...args) => { | ||
| if (mode.current === 'optimistic' || mode.current === 'undoable') { | ||
| const [, , onMutateResult] = args; | ||
| // If the mutation fails, use the context returned from onMutate to rollback | ||
| context.snapshot.forEach(([key, value]) => { | ||
| queryClient.setQueryData(key, value); | ||
| }); | ||
| (onMutateResult as { snapshot: Snapshot }).snapshot.forEach( | ||
| ([key, value]) => { | ||
| queryClient.setQueryData(key, value); | ||
| } | ||
| ); | ||
| } | ||
| if (callTimeOnError.current) { | ||
| return callTimeOnError.current(error, variables, context); | ||
| return callTimeOnError.current(...args); | ||
| } | ||
| if (mutationOptions.onError) { | ||
| return mutationOptions.onError(error, variables, context); | ||
| return mutationOptions.onError(...args); | ||
| } | ||
| // call-time error callback is executed by react-query | ||
| }, | ||
| onSuccess: ( | ||
| data: ResultRecordType, | ||
| variables: Partial<UseCreateMutateParams<RecordType>> = {}, | ||
| context: unknown | ||
| ) => { | ||
| onSuccess: (...args) => { | ||
| if (mode.current === 'pessimistic') { | ||
| const [data, variables] = args; | ||
| const { resource: callTimeResource = resource } = variables; | ||
| queryClient.setQueryData( | ||
| [callTimeResource, 'getOne', { id: String(data.id) }], | ||
|
|
@@ -243,38 +241,26 @@ export const useCreate = < | |
| mutationOptions.onSuccess && | ||
| !hasCallTimeOnSuccess.current | ||
| ) { | ||
| mutationOptions.onSuccess(data, variables, context); | ||
| mutationOptions.onSuccess(...args); | ||
| } | ||
| } | ||
| }, | ||
| onSettled: ( | ||
| data, | ||
| error, | ||
| variables, | ||
| context: { snapshot: Snapshot } | ||
| ) => { | ||
| onSettled: (...args) => { | ||
| if (mode.current === 'optimistic' || mode.current === 'undoable') { | ||
| const [, , , onMutateResult] = args; | ||
| // Always refetch after error or success: | ||
| context.snapshot.forEach(([queryKey]) => { | ||
| queryClient.invalidateQueries({ queryKey }); | ||
| }); | ||
| (onMutateResult as { snapshot: Snapshot }).snapshot.forEach( | ||
| ([queryKey]) => { | ||
| queryClient.invalidateQueries({ queryKey }); | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| if (callTimeOnSettled.current) { | ||
| return callTimeOnSettled.current( | ||
| data, | ||
| error, | ||
| variables, | ||
| context | ||
| ); | ||
| return callTimeOnSettled.current(...args); | ||
| } | ||
| if (mutationOptions.onSettled) { | ||
| return mutationOptions.onSettled( | ||
| data, | ||
| error, | ||
| variables, | ||
| context | ||
| ); | ||
| return mutationOptions.onSettled(...args); | ||
| } | ||
| }, | ||
| }); | ||
|
|
@@ -409,7 +395,12 @@ export const useCreate = < | |
| onSuccess( | ||
| callTimeData as unknown as ResultRecordType, | ||
| { resource: callTimeResource, ...callTimeParams }, | ||
| { snapshot: snapshot.current } | ||
| { snapshot: snapshot.current }, | ||
| { | ||
| client: queryClient, | ||
| mutationKey: [resource, 'create', params], | ||
| meta: mutationOptions.meta, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here however I wonder if you shouldn't use
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope, that's react-query meta, not react-admin meta
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know we actually override them but this is the 4th parameter introduced in latest react-query and I think we should follow the same rule as the query key here |
||
| } | ||
| ); | ||
| } else if ( | ||
| mutationOptions.onSuccess && | ||
|
|
@@ -418,7 +409,12 @@ export const useCreate = < | |
| mutationOptions.onSuccess( | ||
| callTimeData as unknown as ResultRecordType, | ||
| { resource: callTimeResource, ...callTimeParams }, | ||
| { snapshot: snapshot.current } | ||
| { snapshot: snapshot.current }, | ||
| { | ||
| client: queryClient, | ||
| mutationKey: [resource, 'create', params], | ||
| meta: mutationOptions.meta, | ||
| } | ||
| ); | ||
| } | ||
| }, 0); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While reviewing I wondered if we shouldn't use
callTimeParamshere instead, but I realize that we use the samemutationKeywhile creating the mutation, which may (or may not) be wrong.In any case, at least it's consistent, so I guess we can ignore this for the context of this PR...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be the same query key as the one passed to
useMutationotherwise it will cause issue with offline as you won't be able to target the mutation