In my model a person can have multiple pets:
[
{
id: 1,
name: 'liam',
pets: [
{
id: 1,
animal: 'cat',
},
{
id: 2,
animal: 'dog',
}
]
},
{
id: 2,
name: 'keith',
pets: [
{
id: 3,
animal: 'spider',
},
]
},
]
I have implemented following api
const extendedApi = api.injectEndpoints({
endpoints: (builder) => ({
postPersonPet: builder.mutation({
query: ({ personName, ...body }) => ({
url: () => `/persons/${personName}/pets`,
method: 'POST',
body,
}),
// I want to invalidate pets of a specific personName
invalidatesTags: (result, error, { personName }) => [{ type: 'PETS_TAG', personName: personName, id: `LIST` }],
}),
getPersonPets: builder.query({
query: ({ personName }) => `/persons/${personName}/pets`,
providesTags: (result, error, { personName }) => {
if (!result) return [{ type: 'PETS_TAG', personName: personName, id: 'LIST' }]
const tags = (result.length > 0) ?
// I want provide tags of pets of a specific personName
result.map((pet) => ({ type: 'PETS_TAG', personName: personName, id: pet.id }))
:
[{ type: 'PETS_TAG', personName: personName, id: 'LIST' }]
return tags
}
}),
}),
})
When I create a new pet using POST /persons/liam/pets, I want to invalidate the pets of person with name liam. What's the correct way to do it?