Overview
TheQueryClient is the core class that manages queries, mutations, and their caches. It provides methods for fetching, caching, and synchronizing data throughout your application.
Constructor
QueryClientConfig
Configuration options for the QueryClient
Methods
mount()
Mounts the QueryClient, setting up listeners for focus and online events.This method is called automatically when using framework adapters like React Query. Manual invocation is typically not needed.
unmount()
Unmounts the QueryClient and cleans up listeners.fetchQuery()
Fetches a query and returns a promise that resolves with the data.FetchQueryOptions<TQueryFnData, TError, TData, TQueryKey>
required
Promise<TData>
Example:
prefetchQuery()
Prefetches a query and caches the result. Does not return data or throw errors.fetchInfiniteQuery()
Fetches an infinite query with pagination support.Promise<InfiniteData<TData, TPageParam>>
prefetchInfiniteQuery()
Prefetches an infinite query.getQueryData()
Returns the cached data for a query, orundefined if the query does not exist.
This is a non-reactive way to retrieve data. Use
useQuery in React components to subscribe to changes.setQueryData()
Updates the cached data for a query.QueryKey
required
Query key to update
TQueryFnData | Updater<TQueryFnData>
required
New data or function that receives old data and returns new data
SetDataOptions
getQueriesData()
Returns cached data for multiple queries matching the filters.[queryKey, data] tuples
setQueriesData()
Updates cached data for multiple queries matching the filters.getQueryState()
Returns the full state of a query.object
ensureQueryData()
Ensures that query data is available. If data exists and is not stale, it returns the cached data. Otherwise, it fetches the data.boolean
If
true, will refetch in the background if data is stale. Defaults to false.invalidateQueries()
Marks queries as stale and optionally refetches them.InvalidateQueryFilters
refetchQueries()
Refetches queries that match the filters.RefetchOptions
cancelQueries()
Cancels ongoing queries that match the filters.boolean
Revert the query to its previous state. Defaults to
true.removeQueries()
Removes queries from the cache.resetQueries()
Resets queries to their initial state and optionally refetches them.isFetching()
Returns the number of queries that are currently fetching.isMutating()
Returns the number of mutations that are currently pending.getQueryCache()
Returns the QueryCache instance.getMutationCache()
Returns the MutationCache instance.getDefaultOptions()
Returns the default options for the QueryClient.setDefaultOptions()
Sets default options for all queries and mutations.setQueryDefaults()
Sets default options for queries with specific query keys.getQueryDefaults()
Returns the default options for a specific query key.setMutationDefaults()
Sets default options for mutations with specific mutation keys.getMutationDefaults()
Returns the default options for a specific mutation key.resumePausedMutations()
Resumes all paused mutations.clear()
Clears all queries and mutations from the cache.This will remove all data from the cache and should be used with caution.
Type Parameters
Most methods accept the following type parameters:TQueryFnData- The type of data returned by the query functionTError- The type of error that can be thrown (defaults toDefaultError)TData- The type of data after selection/transformationTQueryKey- The type of the query keyTPageParam- The type of the page parameter for infinite queries