Custom api endpoint with useUpdate

Is it possible to provide mutate a custom api endpoint when using the useUpdate data hook? I want to get the flexibility of useCustom but be able to make the call in response to a user action like pressing a button.
2 Replies
kapa.ai
kapa.ai2mo ago
Hi @Molag's Meat. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Yes, it is possible to provide a custom API endpoint when using the useUpdate data hook in refine. You can achieve this by specifying the resource parameter, which will be passed to the update method from the dataProvider as a parameter. This parameter is usually used as an API endpoint path. Here is an example of how you can use the useUpdate hook with a custom API endpoint:
import { useUpdate } from "@refinedev/core";

const { mutate } = useUpdate();

mutate({
resource: "custom-endpoint", // specify your custom API endpoint here
id: 1, // specify the ID of the record to update
values: {
title: "Updated Title",
},
});
import { useUpdate } from "@refinedev/core";

const { mutate } = useUpdate();

mutate({
resource: "custom-endpoint", // specify your custom API endpoint here
id: 1, // specify the ID of the record to update
values: {
title: "Updated Title",
},
});
In this example, the resource parameter is set to "custom-endpoint", which will be used as the API endpoint path. The id parameter specifies the record to update, and the values parameter contains the data to update. For more information, you can refer to the useUpdate documentation (https://refine.dev/docs/data/hooks/use-update/#resource-). If you need more flexibility similar to useCustom, you can also consider using the useCustomMutation hook, which allows you to specify both the url and method parameters:
import { useCustomMutation } from "@refinedev/core";

const { mutate } = useCustomMutation();

mutate({
url: "www.example.com/api/update-products", // specify your custom API endpoint here
method: "post", // specify the HTTP method
});
import { useCustomMutation } from "@refinedev/core";

const { mutate } = useCustomMutation();

mutate({
url: "www.example.com/api/update-products", // specify your custom API endpoint here
method: "post", // specify the HTTP method
});
For more details, refer to the useCustomMutation documentation (https://refine.dev/docs/data/hooks/use-custom-mutation/#url-). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2mo ago
<@224018850419572736> kapa.ai is still learning and improving, please let us know how it did by reacting below