national-gold
national-gold3mo ago

Invalidating a nested resource after an update REST request

If my getOne api route is /teams/{teamId}/members/{memberId} its resource is '/teams/{teamId}/members' I have a route that updates the member via /teams/{teamId}/members/{memberId}/roles which i want to invalidate '/teams/{teamId}/members' but if i were to give it { resource: '/teams/{teamId}/members', id: memberId }, how do i append '/roles' to the end of the url?
5 Replies
kapa.ai
kapa.ai3mo ago
Hi @Angelina. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- In Refine, the useInvalidate hook is used to invalidate the state of a particular resource. However, it doesn't directly support appending a path to the end of the URL. It works by invalidating the cache of a specific resource or a specific item of a resource. Here's how you can use it:
invalidate({
resource: "/teams/{teamId}/members",
invalidates: ["detail"],
id: memberId,
});
invalidate({
resource: "/teams/{teamId}/members",
invalidates: ["detail"],
id: memberId,
});
In this case, it will invalidate the detail state of the member with the given memberId in the /teams/{teamId}/members resource. If you want to invalidate the list of members after updating the roles, you can do:
invalidate({
resource: "/teams/{teamId}/members",
invalidates: ["list", "many"],
});
invalidate({
resource: "/teams/{teamId}/members",
invalidates: ["list", "many"],
});
This will invalidate the list and many states of the /teams/{teamId}/members resource. Unfortunately, appending '/roles' to the end of the URL is not supported by the useInvalidate hook. The hook is designed to work with the resource and id parameters to target specific data states to invalidate. For more information, you can check the Refine documentation on useInvalidate (https://refine.dev/docs/data/hooks/use-invalidate#examples). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3mo ago
<@405229347918577664> kapa.ai is still learning and improving, please let us know how it did by reacting below
national-gold
national-gold3mo ago
so in my useUpdate's mutate, I'll use
{ resource: '/teams/{teamId}/members/{memberId}/roles', id: '' }
{ resource: '/teams/{teamId}/members/{memberId}/roles', id: '' }
and then onSuccess, manually invalidate '/teams/{teamId}/members'? @kapa.ai
kapa.ai
kapa.ai3mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- Yes, you're correct. In your useUpdate's mutate, you can specify the resource as /teams/{teamId}/members/{memberId}/roles and then manually invalidate /teams/{teamId}/members on success. Here's how you can do it:
import { useUpdate, useInvalidate } from "@refinedev/core";

const { mutate } = useUpdate();
const invalidate = useInvalidate();

mutate({
resource: "/teams/{teamId}/members/{memberId}/roles",
id: "",
onMutationSuccess: () => {
invalidate({
resource: "/teams/{teamId}/members",
invalidates: ["list", "many"],
});
},
});
import { useUpdate, useInvalidate } from "@refinedev/core";

const { mutate } = useUpdate();
const invalidate = useInvalidate();

mutate({
resource: "/teams/{teamId}/members/{memberId}/roles",
id: "",
onMutationSuccess: () => {
invalidate({
resource: "/teams/{teamId}/members",
invalidates: ["list", "many"],
});
},
});
In this code, useUpdate is used to perform the update operation on the /teams/{teamId}/members/{memberId}/roles resource. Then, in the onMutationSuccess callback, the useInvalidate hook is used to invalidate the /teams/{teamId}/members resource. For more information, you can check the Refine documentation on useUpdate (https://refine.dev/docs/data/hooks/use-update#invalidates) and useInvalidate (https://refine.dev/docs/data/hooks/use-invalidate#examples). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3mo ago
<@405229347918577664> kapa.ai is still learning and improving, please let us know how it did by reacting below