What's the recommended way to handle API requests that aren't closely-couped with a resource?

For example, I have an API request that enqueues an async job that updates multiple entities.
2 Replies
kapa.ai
kapa.ai3w ago
Hi @arkady. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To handle API requests that aren't closely coupled with a resource in refine, the recommended approach is to use the useCustom hook along with a custom API endpoint. This method is ideal for scenarios like enqueuing an async job that updates multiple entities. Here's how you can implement this: 1. First, define a custom endpoint in your data provider. You can do this by implementing the custom method in your data provider. 2. Then, use the useCustom hook in your component to make the API request. Here's an example of how you might use useCustom:
import { useCustom } from "@refinedev/core";

const MyComponent = () => {
const { mutate } = useCustom({
url: "/api/enqueue-job",
method: "post",
});

const handleEnqueueJob = () => {
mutate({
payload: {
// Your job parameters here
},
});
};

return <button onClick={handleEnqueueJob}>Enqueue Job</button>;
};
import { useCustom } from "@refinedev/core";

const MyComponent = () => {
const { mutate } = useCustom({
url: "/api/enqueue-job",
method: "post",
});

const handleEnqueueJob = () => {
mutate({
payload: {
// Your job parameters here
},
});
};

return <button onClick={handleEnqueueJob}>Enqueue Job</button>;
};
This approach is flexible and allows you to handle various types of custom API requests that don't fit into the standard CRUD operations. For more details on using useCustom, you can refer to the refine data hooks documentation. Remember to implement the corresponding custom method in your data provider to handle these custom requests on the backend. Data Provider documentation provides more information on implementing the custom method in your data provider. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3w ago
<@116305505097482243> Kapa.ai is still learning and improving, please let me know how I did by reacting below.

Did you find this page helpful?