Fetch and cache data with the createQuery function. It returns a reactive query result that automatically updates when dependencies change in Svelte 5’s runes mode.
Signature
Parameters
options Accessor<CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey>>
required
A function (accessor) returning query configuration options. Unique identifier for the query. Changes trigger automatic refetch.
queryFn QueryFunction<TQueryFnData, TQueryKey>
required
Function that fetches the data.
Set to false to disable automatic query execution.
Time in milliseconds until cached data is considered stale.
Time in milliseconds before unused data is garbage collected.
Interval in milliseconds for automatic refetching.
Refetch when window regains focus.
Refetch when network reconnects.
Number of retry attempts or boolean to enable/disable retries.
select (data: TQueryData) => TData
Transform or select a part of the data.
Initial data to use before the query executes.
Placeholder data while the query is loading.
Accessor returning a custom QueryClient instance. If not provided, uses the client from context.
Returns
CreateQueryResult<TData, TError> Reactive query state accessible via Svelte runes. The query result data. Returns undefined if not yet loaded.
The error object if the query failed.
true when fetching for the first time (no cached data).
true whenever the query is fetching (including background refetches).
true when the query has successfully fetched data.
true when the query encountered an error.
true when the query is pending (no data and no error yet).
status 'pending' | 'error' | 'success'
The current status of the query.
fetchStatus 'fetching' | 'paused' | 'idle'
The fetch status of the query.
refetch () => Promise<QueryObserverResult>
Manually trigger a refetch of the query.
Type Parameters
TQueryFnData - Type returned by the query function
TError - Type of error (defaults to DefaultError)
TData - Type of data returned (defaults to TQueryFnData)
TQueryKey - Type of the query key (defaults to QueryKey)
Examples
Basic Usage (Svelte 5 Runes)
Reactive Query Keys
With TypeScript
Conditional Fetching
With Select
Dependent Queries
Initial Data
Polling/Refetch Interval
Manual Refetch
Error Handling
Multiple Reactive Dependencies
With Loading States
Notes
Svelte Query uses Svelte 5’s runes mode. The options parameter must be an accessor (function) to track reactive dependencies.
Access query state properties directly (e.g., query.data, query.isLoading) without needing to use $state or stores.
Make sure to use Svelte 5 with runes mode enabled. Svelte Query v5+ requires Svelte 5.