I want to achieve the behavior where after successful mutation, all queries with dependent data should be marked as invalid but only active should be refetched right away, and inactive should be refetched only whether they would become active.
As I understand the react-query API, this behavior can be achieved using
queryClient.invalidateQueries(['query-key'], { refetchType: 'active' })
as it invalidates all queries that match but refetch only active ones, but this is not refetch invalid inactive data when it becomes active. Maybe it can be a problem not only in the way invalidation is called but also in some default query options.
Currently, I'm simply refetch all invalid queries, whether they are active or not, but this is quite not the right approach.
// query client defaults
const queryClient = new QueryClient({
defaultOptions: {
queries: {
retry: false,
refetchOnMount: false,
refetchOnReconnect: false,
refetchOnWindowFocus: false,
},
},
})
// mutation with query invalidation
const useCreateExample = () => {
const queryClient = useQueryClient()
return useMutation({
mutationFn: createExample,
onSuccess: () => {
queryClient.invalidateQueries(['example'], { refetchType: 'all' })
},
})
}