Filters

· abundance's blog


Some methods within TanStack Query accept a QueryFilters or MutationFilters object.

Query Filters #

A query filter is an object with certain conditions to match a query with:

 1// Cancel all queries
 2await queryClient.cancelQueries()
 3
 4// Remove all inactive queries that begin with `posts` in the key
 5queryClient.removeQueries({ queryKey: ['posts'], type: 'inactive' })
 6
 7// Refetch all active queries
 8await queryClient.refetchQueries({ type: 'active' })
 9
10// Refetch all active queries that begin with `posts` in the key
11await queryClient.refetchQueries({ queryKey: ['posts'], type: 'active' })

A query filter object supports the following properties:

Mutation Filters #

A mutation filter is an object with certain conditions to match a mutation with:

 1// Get the number of all fetching mutations
 2await queryClient.isMutating()
 3
 4// Filter mutations by mutationKey
 5await queryClient.isMutating({ mutationKey: ['post'] })
 6
 7// Filter mutations using a predicate function
 8await queryClient.isMutating({
 9  predicate: (mutation) => mutation.options.variables?.id === 1,
10})

A mutation filter object supports the following properties:

Utils #

matchQuery #

1const isMatching = matchQuery(filters, query)

Returns a boolean that indicates whether a query matches the provided set of query filters.

matchMutation #

1const isMatching = matchMutation(filters, mutation)

Returns a boolean that indicates whether a mutation matches the provided set of mutation filters.