useMutation()
The Hook enables you to perform asynchronous data mutation operations, such as POST, PUT, PATCH, or DELETE requests. It not only handles the loading state, but also provides an APIs for optimistic updates and error handling. See the TanStack useMutation(...) ๐ด documentation for more details.
const query = qraft.<service>.<operation>.useMutation(
parameters,
mutationOptions,
queryClient,
)
Argumentsโ
-
parameters?: { path, query, header }
- Optional, OpenAPI request parameters for the query, strictly-typed โจ
- When
parameters
provided, it will be used to generate themutationKey
-
mutationOptions?: UseMutationOptions
- Optional, represents the options of the useMutation(...) ๐ด Hook
- When
qraft.<srv>.<op>.useMutation(parameters)
is used (with the predefined parameters),mutationKey
will contain theparameters
and operation schema - When
qraft.<srv>.<op>.useMutation(undefined)
is used (without predefined parameters),mutationKey
will contain just operation schema mutationOptions.mutationFn
could be provided to override the defaultmutationFn
used by Qraft, strictly-typed โจmutationOptions.mutationKey
could be provided to override the generated key fromparameters
, strictly-typed โจ
-
queryClient?: QueryClient
- Optional QueryClient ๐ด to be used
- If not provided
QraftContext.queryClient
will be used if available- useQueryClient() ๐ด result will be used as a fallback
Returnsโ
mutation: MutationResult
- Object from the useMutation(...) ๐ด Hook
mutation.mutate(parameters | {parameters, body})
initiates the requestmutation.variables
will contain theparameters
andbody
of the request, after callingmutation.mutate(...)
.
Examplesโ
- With parameters ๐
- No parameters ๐
When the parameters
are known, the useMutation
hook can be used with parameters
to generate the mutationKey
.
mutation.mutate(...)
will accept thebody
of the request as the single argument.
src/EntityEditForm.tsx
import { createAPIClient } from './api'; // generated by OpenAPI Qraft
const qraft = createAPIClient();
function EntityEditForm({ entityId }: { entityId: string }) {
const mutation = qraft.entities.entitiesIdDocuments.useMutation({
path: {
entity_id: entityId
},
header: {
'x-monite-version': '2023-09-01',
},
});
return (
<form
onSubmit={(event) => {
event.preventDefault();
const formData = new FormData(event.currentTarget);
/**
* `mutation.mutate(...)` triggers the submission of a request:
* ###
* POST /entities/3e3e-3e3e-3e3e/documents
* x-monite-version: 2023-09-01
*
* {"company_tax_id_verification": ["verification-id"]}
**/
mutation.mutate({
company_tax_id_verification: [
`${formData.get('company_tax_id_verification')}`,
],
});
}}
>
<input name="company_tax_id_verification" />
<button disabled={mutation.isPending}>Submit</button>
</form>
);
}
When the parameters
are not known, the useMutation
hook can be used without parameters.
mutation.mutate(...)
will accept the object withparameters
andbody
of the request as the single argument.
src/EntityForm.tsx
import { createAPIClient } from './api'; // generated by OpenAPI Qraft
const qraft = createAPIClient();
function EntityForm() {
const mutation = qraft.entities.entitiesIdDocuments.useMutation();
return (
<form
onSubmit={(event) => {
event.preventDefault();
const formData = new FormData(event.currentTarget);
/**
* `mutation.mutate(...)` triggers the submission of a request:
* ###
* POST /entities/3e3e-3e3e-3e3e/documents
* x-monite-version: 2023-09-01
*
* {"company_tax_id_verification": ["verification-id"]}
**/
mutation.mutate({
parameters: {
path: {
entity_id: `${formData.get('entity_id')}`
},
header: {
'x-monite-version': '2023-09-01',
},
},
body: {
company_tax_id_verification: [
`${formData.get('company_tax_id_verification')}`,
],
}
});
}}
>
<input name="entity_id" readOnly={mutation.isPending} />
<input name="company_tax_id_verification" readOnly={mutation.isPending} />
<button disabled={mutation.isPending}>Submit</button>
</form>
);
}