I am trying to use useMutation for my graphql API but it does not seems to make any call,
here is the code
export async function bulkImport(importRequest: BulkImportRequest): Promise<string> {
const [postBulkImport, { data }] = useMutation(postBulkImportRequest, {
variables: importRequest,
});
await postBulkImport({ variables: importRequest });
return data?.requestId;
}
and the I call bulkImport in my React component.
I am trying to rewrite below code using useMutation instead of mutate. Bellow code is working fine.
export async function bulkImport(importRequest: BulkImportRequest): Promise<string> {
const response = await mutate(postBulkImportRequest, importRequest);
return response?.data?.postBulkImportRequest?.requestId;
}
I am not sure what I am missing when I use useMutation, I am actually not sure if they are the same.
any advice will be appreciated
useMutation is a React Hook and as such can only be used from the top level of a React functional component.
Nothing happens when you try and use it from your bulkImport function because it's not at the top level of a React component.
You'll need to (quite dramatically) rewrite your component in order to work with useMutation successfully.