Skip to main content
Default query functions allow you to define a global query function that’s used when no queryFn is specified. This is useful for standardizing data fetching across your application.

Setting a Default Query Function

Configure a default query function when creating the QueryClient:
With a default query function, you can omit the queryFn option in individual queries.

Using Queries Without queryFn

Once configured, use queries without specifying queryFn:
Structure your query keys to match your API endpoints when using a default query function.

Overriding the Default

You can still provide a custom queryFn for specific queries:

Advanced Default Function

Create a more sophisticated default function with error handling and authentication:

Using with TypeScript

Type your default query function for better type safety:

Query-Specific Defaults

Set defaults for specific query keys using setQueryDefaults:
Source: packages/query-core/src/queryClient.ts:473
1

Define query defaults

Use setQueryDefaults with a query key prefix
2

Defaults apply to matching keys

Any query with a matching key prefix inherits these defaults
3

Use queries without options

Queries automatically use the configured defaults

Partial Matching

Query defaults use partial key matching:
Query defaults match by prefix. The defaults key must be a prefix of the query key, not vice versa.
Source: packages/query-core/src/queryClient.ts:493

Getting Query Defaults

Retrieve defaults for a specific query key:

Merging with Other Defaults

Query defaults merge with global defaults and query-specific options:

Real-World Example

A complete setup for a REST API:

When to Use Default Query Functions

Use default query functions when:
  • Your API follows consistent patterns
  • You want to centralize auth, error handling, or logging
  • You have many similar queries
  • You want to reduce boilerplate
Avoid when:
  • Your queries fetch from different sources
  • Each query needs unique logic
  • You prefer explicit queryFn declarations

See Also