-
When manipulating the cache, I find that Imagine I have an arbitrary number of queries on the same entity: const useQueryOne = () =>
useQuery({
queryFn: () => axios.request({url: '/api/endpoint', method: 'GET'}),
queryKey: ['endpoint'],
})
const useQueryTwo = () =>
useQuery({
queryFn: () => axios.request({url: '/api/endpoint', method: 'GET', params: { include: { documents: true } }}),
queryKey: ['endpoint', 'documents'],
})
const useQueryThree = () =>
useQuery({
queryFn: () => axios.request({url: '/api/endpoint', method: 'GET', params: { include: { records: true } }}),
queryKey: ['endpoint', 'records'],
}) When mutating an entity associated with What that looks like right now: const useMutationOne = () =>
const queryClient = useQueryClient()
useMutation({
mutationFn: ({id}) => axios.request({url: `/api/endpoint/${id}`}),
onSuccess: (data) => {
queryClient.setQueryData(['endpoint', 'records'], () => /* some update logic */)
queryClient.invalidateQueries(['endpoint'], { exact: true })
queryClient.invalidateQueries(['endpoint', 'documents'], { exact: true })
},
}) This example is a little contrived but with an arbitrary amount of various queries on the same endpoint, it could quickly become messy. What that could look like with an const useMutationOne = () =>
const queryClient = useQueryClient()
useMutation({
mutationFn: ({id}) => axios.request({url: `/api/endpoint/${id}`}),
onSuccess: (data) => {
queryClient.setQueryData(['endpoint', 'records'], () => /* some update logic */)
queryClient.invalidateQueries(['endpoint'], { exclude: [['endpoint', 'records']] })
},
}) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
calling
Please note that Granted, I'm not sure that this is a lot better, but it can handle the arbitrary amount of queries under the Also, predicate is not that bad, you'd just need to do a negative match for the one key you'd like to exclude. |
Beta Was this translation helpful? Give feedback.
calling
invalidateQueries
marks the query as invalid, but callingsetQueryData
actually marks it asvalid
again because you successfully give it data. With that in mind, what you can do is:Please note that
refetchType
is v4 syntax already, in v3, it would berefetchActive: false
I think.Granted, I'm not sure that this is a lot better, but it can handle the arbitra…