Find cached queries matching a key or other criteria
Cache inspection
TanStack · Function
Tests whether a Query satisfies the supplied QueryFilters. It returns true only when every provided filter matches.
matchQuery evaluates one Query against a QueryFilters object. Every supplied filter must match; omitted filters do not constrain the result, and the function returns a boolean.
function matchQuery(filters: QueryFilters, query: Query<any, any, any, any>): boolean;| Name | Type | Description |
|---|---|---|
filters | QueryFilters | Filters used to test the query; supported properties include exact, fetchStatus, predicate, queryKey, stale, and type. |
query | Query<any, any, any, any> | The query to evaluate against the filters. |
boolean — true if the query matches every specified filter.
import { matchQuery } from '@tanstack/query-core'
const isMatch = matchQuery(
{ queryKey: ['posts'], exact: false },
query,
)
console.log(isMatch)Pass a QueryFilters object and a Query instance to test whether the query satisfies every specified filter.
Cache inspection
Selective cache operations
Custom selection
matchQuery accepts QueryFilters and a Query, then returns true only when the query satisfies every specified filter. Unspecified filters are ignored.
Follow-up: How can you inspect a cache for matches? Retrieve all queries from the QueryCache and filter them with matchQuery.
Set queryKey in the filters to test the query key. The exact option controls whether the key must match exactly rather than using a matching prefix.
Follow-up: What other query-selection dimensions are available? Use type, stale, fetchStatus, or predicate filters.
The function returns a boolean indicating whether the query matches all requested criteria. Its second argument is a Query instance, not a query key or query data object.
Follow-up: How would you select only active stale queries? Provide type: 'active' and stale: true in the filters.
QueryFiltersQueryQueryCachequeryClient.getQueryCache