@@ -93,6 +93,7 @@ export const useCreate = <
9393 const {
9494 mutationMode = 'pessimistic' ,
9595 getMutateWithMiddlewares,
96+ mutationFn : customMutationFn ,
9697 onSettled,
9798 ...mutationOptions
9899 } = options ;
@@ -103,6 +104,26 @@ export const useCreate = <
103104 params as CreateParams < RecordType >
104105 )
105106 ) ;
107+ const customMutationFnWithDataProviderResult = async (
108+ resource : string | undefined ,
109+ params : Omit < UseCreateMutateParams < RecordType > , 'resource' >
110+ ) => {
111+ if ( resource == null ) {
112+ throw new Error ( 'useCreate mutation requires a resource' ) ;
113+ }
114+ if ( params . data == null ) {
115+ throw new Error (
116+ 'useCreate mutation requires a non-empty data object'
117+ ) ;
118+ }
119+ if ( customMutationFn == null ) {
120+ return dataProviderCreate ( resource , params ) ;
121+ }
122+
123+ return {
124+ data : await customMutationFn ( { resource, ...params } ) ,
125+ } ;
126+ } ;
106127
107128 const [ mutate , mutationResult ] = useMutationWithMutationMode <
108129 MutationError ,
@@ -114,17 +135,8 @@ export const useCreate = <
114135 ...mutationOptions ,
115136 mutationKey : [ resource , 'create' , params ] ,
116137 mutationMode,
117- mutationFn : ( { resource, ...params } ) => {
118- if ( resource == null ) {
119- throw new Error ( 'useCreate mutation requires a resource' ) ;
120- }
121- if ( params . data == null ) {
122- throw new Error (
123- 'useCreate mutation requires a non-empty data object'
124- ) ;
125- }
126- return dataProviderCreate ( resource , params ) ;
127- } ,
138+ mutationFn : ( { resource, ...params } ) =>
139+ customMutationFnWithDataProviderResult ( resource , params ) ,
128140 updateCache : (
129141 { resource, ...params } ,
130142 { mutationMode } ,
@@ -179,12 +191,21 @@ export const useCreate = <
179191
180192 return queryKeys ;
181193 } ,
182- getMutateWithMiddlewares : mutationFn => {
194+ getMutateWithMiddlewares : mutateWithMutationMode => {
183195 if ( getMutateWithMiddlewares ) {
184196 // Immediately get the function with middlewares applied so that even if the middlewares gets unregistered (because of a redirect for instance),
185197 // we still have them applied when users have called the mutate function.
186198 const mutateWithMiddlewares = getMutateWithMiddlewares (
187- dataProviderCreate . bind ( dataProvider )
199+ customMutationFn
200+ ? ( resource , params ) =>
201+ customMutationFnWithDataProviderResult (
202+ resource ,
203+ params as Omit <
204+ UseCreateMutateParams < RecordType > ,
205+ 'resource'
206+ >
207+ )
208+ : dataProviderCreate . bind ( dataProvider )
188209 ) ;
189210 return args => {
190211 // This is necessary to avoid breaking changes in useCreate:
@@ -194,7 +215,7 @@ export const useCreate = <
194215 } ;
195216 }
196217
197- return args => mutationFn ( args ) ;
218+ return args => mutateWithMutationMode ( args ) ;
198219 } ,
199220 onUndo : ( { resource, data, meta } ) => {
200221 queryClient . removeQueries ( {
@@ -268,6 +289,9 @@ export type UseCreateOptions<
268289 > ,
269290 'mutationFn'
270291> & {
292+ mutationFn ?: (
293+ params : Partial < UseCreateMutateParams < RecordType > >
294+ ) => Promise < ResultRecordType > ;
271295 mutationMode ?: MutationMode ;
272296 returnPromise ?: boolean ;
273297 getMutateWithMiddlewares ?: <
0 commit comments