Skip to content

raf-query is early. The API can still change between minor releases, so use it at your own risk.

raf-query

Guides

Query functions

What a queryFn may return, how errors are reported, and what the query function context contains.

A query function can be literally any function that returns a value or a promise. The promise should either resolve the data or throw.

Unlike TanStack Query, undefined is a valid result: a queryFn typed Promise<User | undefined> caches undefined like any other value. So getData and a setData updater's previous are undefined both with no data and with undefined data; a reader's isPending tells them apart.

All of these are valid:

infiniteQueryOptions<readonly ["posts"], PostPage, number, InfiniteData<PostPage, number>>(options: InfiniteOptions<readonly ["posts"], PostPage, number, InfiniteData<PostPage, number>> & Partial<{
    readonly retry: number | false | undefined;
    readonly refetchOnWindowFocus: boolean | undefined;
    readonly refetchOnReconnect: boolean | undefined;
    readonly refetchInterval: number | false | undefined;
}> & {
    readonly queryFn: (context: InfiniteQueryFunctionContext<readonly ["posts"], number, {
        readonly "~options": {
            readonly retry: (value: number | false | undefined) => number | ... 1 more ... | undefined;
            readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
            readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
            readonly refetchInterval: (value: number | ... 1 more ... | undefined) => number | ... 1 more ... | undefined;
        };
        readonly context: {};
        readonly options: {
            ...;
        };
    }>) => PostPage | PromiseLike<...>;
}): InfiniteQueryOptions<...>
infiniteQueryOptions
({
BaseOptions<readonly ["posts"]>.queryKey: readonly ["posts"]

A plain array, usually built by a group(). Handles keep its literal type.

queryKey
:
const posts: Group
<readonly []>() => readonly ["posts"]
posts
(),
queryFn: (context: InfiniteQueryFunctionContext<readonly ["posts"], number, {
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}>) => PostPage | PromiseLike<...>
queryFn
: (
context: InfiniteQueryFunctionContext<readonly ["posts"], number, {
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}>
context
) => function fetchPostPage(cursor: number): Promise<PostPage>fetchPostPage(
context: InfiniteQueryFunctionContext<readonly ["posts"], number, {
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}>
context
.pageParam: numberpageParam),
InfiniteOptions<readonly ["posts"], PostPage, number, InfiniteData<PostPage, number>>.initialPageParam: numberinitialPageParam: 0, InfiniteOptions<readonly ["posts"], PostPage, number, InfiniteData<PostPage, number>>.getNextPageParam: PageParamFn<PostPage, number>getNextPageParam: (lastPage: PostPagelastPage) => lastPage: PostPagelastPage.PostPage.nextCursor: number | nullnextCursor, });
queryOptions<readonly ["posts", string], Post, Post>(options: BaseOptions<readonly ["posts", string]> & Partial<{
    readonly retry: number | false | undefined;
    readonly refetchOnWindowFocus: boolean | undefined;
    readonly refetchOnReconnect: boolean | undefined;
    readonly refetchInterval: number | false | undefined;
}> & {
    readonly queryFn: (context: QueryFunctionContext<readonly ["posts", string], {
        readonly "~options": {
            readonly retry: (value: number | false | undefined) => number | false | undefined;
            readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
            readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
            readonly refetchInterval: (value: number | ... 1 more ... | undefined) => number | ... 1 more ... | undefined;
        };
        readonly context: {};
        readonly options: {
            ...;
        };
    }>) => Awaitable<...>;
    readonly select?: ((data: Post) => Post) | undefined;
}): QueryOptions<...> (+1 overload)
queryOptions
({ BaseOptions<readonly ["posts", string]>.queryKey: readonly ["posts", string]

A plain array, usually built by a group(). Handles keep its literal type.

queryKey
:
const posts: Group
<readonly [string]>(rest_0: string) => readonly ["posts", string]
posts
(const slug: stringslug),
queryFn: (context: QueryFunctionContext<readonly ["posts", string], {
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}>) => Awaitable<...>
queryFn
: () => function fetchPostBySlug(slug: string): Promise<Post>fetchPostBySlug(const slug: stringslug) });
queryOptions<readonly ["posts", string], Post, Post>(options: BaseOptions<readonly ["posts", string]> & Partial<{
    readonly retry: number | false | undefined;
    readonly refetchOnWindowFocus: boolean | undefined;
    readonly refetchOnReconnect: boolean | undefined;
    readonly refetchInterval: number | false | undefined;
}> & {
    readonly queryFn: (context: QueryFunctionContext<readonly ["posts", string], {
        readonly "~options": {
            readonly retry: (value: number | false | undefined) => number | false | undefined;
            readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
            readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
            readonly refetchInterval: (value: number | ... 1 more ... | undefined) => number | ... 1 more ... | undefined;
        };
        readonly context: {};
        readonly options: {
            ...;
        };
    }>) => Awaitable<...>;
    readonly select?: ((data: Post) => Post) | undefined;
}): QueryOptions<...> (+1 overload)
queryOptions
({
BaseOptions<readonly ["posts", string]>.queryKey: readonly ["posts", string]

A plain array, usually built by a group(). Handles keep its literal type.

queryKey
:
const posts: Group
<readonly [string]>(rest_0: string) => readonly ["posts", string]
posts
(const slug: stringslug),
queryFn: (context: QueryFunctionContext<readonly ["posts", string], {
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}>) => Awaitable<...>
queryFn
: async () => {
const const data: Postdata = await function fetchPostBySlug(slug: string): Promise<Post>fetchPostBySlug(const slug: stringslug); return const data: Postdata; }, });
queryOptions<readonly ["posts", string], Post, Post>(options: BaseOptions<readonly ["posts", string]> & Partial<{
    readonly retry: number | false | undefined;
    readonly refetchOnWindowFocus: boolean | undefined;
    readonly refetchOnReconnect: boolean | undefined;
    readonly refetchInterval: number | false | undefined;
}> & {
    readonly queryFn: (context: QueryFunctionContext<readonly ["posts", string], {
        readonly "~options": {
            readonly retry: (value: number | false | undefined) => number | false | undefined;
            readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
            readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
            readonly refetchInterval: (value: number | ... 1 more ... | undefined) => number | ... 1 more ... | undefined;
        };
        readonly context: {};
        readonly options: {
            ...;
        };
    }>) => Awaitable<...>;
    readonly select?: ((data: Post) => Post) | undefined;
}): QueryOptions<...> (+1 overload)
queryOptions
({
BaseOptions<readonly ["posts", string]>.queryKey: readonly ["posts", string]

A plain array, usually built by a group(). Handles keep its literal type.

queryKey
:
const posts: Group
<readonly [string]>(rest_0: string) => readonly ["posts", string]
posts
(const slug: stringslug),
queryFn: (context: QueryFunctionContext<readonly ["posts", string], {
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}>) => Awaitable<...>
queryFn
: (
context: QueryFunctionContext<readonly ["posts", string], {
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}>
context
) => function fetchPostBySlug(slug: string): Promise<Post>fetchPostBySlug(
context: QueryFunctionContext<readonly ["posts", string], {
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}>
context
.queryKey: readonly ["posts", string]queryKey[1]),
});

In practice the queryFn lives in a factory and closes over the factory's input, which is easier to type than reading it back from the key.

Handling and throwing errors

A query function that throws or returns a rejected promise fails. The error is kept on the query: the first load's error rejects the promise useQuery returns, so use() throws it to the nearest error boundary, and a later background error shows up in the state's error while the old data stays readable.

queryOptions<readonly ["posts", string], unknown, unknown>(options: BaseOptions<readonly ["posts", string]> & Partial<{
    readonly retry: number | false | undefined;
    readonly refetchOnWindowFocus: boolean | undefined;
    readonly refetchOnReconnect: boolean | undefined;
    readonly refetchInterval: number | false | undefined;
}> & {
    readonly queryFn: (context: QueryFunctionContext<readonly ["posts", string], {
        readonly "~options": {
            readonly retry: (value: number | false | undefined) => number | false | undefined;
            readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
            readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
            readonly refetchInterval: (value: number | ... 1 more ... | undefined) => number | ... 1 more ... | undefined;
        };
        readonly context: {};
        readonly options: {
            ...;
        };
    }>) => unknown;
    readonly select?: ((data: unknown) => unknown) | undefined;
}): QueryOptions<...> (+1 overload)
queryOptions
({
BaseOptions<readonly ["posts", string]>.queryKey: readonly ["posts", string]

A plain array, usually built by a group(). Handles keep its literal type.

queryKey
:
const posts: Group
<readonly [string]>(rest_0: string) => readonly ["posts", string]
posts
(const slug: stringslug),
queryFn: (context: QueryFunctionContext<readonly ["posts", string], {
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}>) => unknown
queryFn
: async () => {
if (const somethingGoesWrong: booleansomethingGoesWrong) { throw new
var Error: ErrorConstructor
new (message?: string, options?: ErrorOptions) => Error (+1 overload)
Error
("Oh no!");
} if (const somethingElseGoesWrong: booleansomethingElseGoesWrong) { return var Promise: PromiseConstructor

Represents the completion of an asynchronous operation

Promise
.PromiseConstructor.reject<never>(reason?: any): Promise<never>

Creates a new rejected promise for the provided reason.

@paramreason — The reason the promise was rejected.@returnsA new rejected Promise.
reject
(new
var Error: ErrorConstructor
new (message?: string, options?: ErrorOptions) => Error (+1 overload)
Error
("Oh no!"));
} return const data: unknowndata; }, });

With retry(), which recommended() includes, failed requests are retried three times before they count as failed.

With fetch and other clients that don't throw

Many clients, such as axios, throw for unsuccessful HTTP responses. fetch doesn't, so throw yourself:

queryOptions<readonly ["posts", string], any, any>(options: BaseOptions<readonly ["posts", string]> & Partial<{
    readonly retry: number | false | undefined;
    readonly refetchOnWindowFocus: boolean | undefined;
    readonly refetchOnReconnect: boolean | undefined;
    readonly refetchInterval: number | false | undefined;
}> & {
    readonly queryFn: (context: QueryFunctionContext<readonly ["posts", string], {
        readonly "~options": {
            readonly retry: (value: number | false | undefined) => number | false | undefined;
            readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
            readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
            readonly refetchInterval: (value: number | ... 1 more ... | undefined) => number | ... 1 more ... | undefined;
        };
        readonly context: {};
        readonly options: {
            ...;
        };
    }>) => any;
    readonly select?: ((data: any) => any) | undefined;
}): QueryOptions<...> (+1 overload)
queryOptions
({
BaseOptions<readonly ["posts", string]>.queryKey: readonly ["posts", string]

A plain array, usually built by a group(). Handles keep its literal type.

queryKey
:
const posts: Group
<readonly [string]>(rest_0: string) => readonly ["posts", string]
posts
(const slug: stringslug),
queryFn: (context: QueryFunctionContext<readonly ["posts", string], {
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}>) => any
queryFn
: async (
context: QueryFunctionContext<readonly ["posts", string], {
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}>
context
) => {
const const response: Responseresponse = await function fetch(input: string | URL | Request, init?: RequestInit): Promise<Response> (+1 overload)fetch(`/posts/${const slug: stringslug}`, { RequestInit.signal?: AbortSignal | null | undefined

An AbortSignal to set request's signal.

signal
:
context: QueryFunctionContext<readonly ["posts", string], {
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}>
context
.signal: AbortSignal

Aborted when the request is restarted or its entry is removed.

signal
});
if (!const response: Responseresponse.Response.ok: boolean

The ok read-only property of the Response interface contains a Boolean stating whether the response was successful (status in the range 200-299) or not.

ok
) {
throw new
var Error: ErrorConstructor
new (message?: string, options?: ErrorOptions) => Error (+1 overload)
Error
(`Request failed: ${const response: Responseresponse.Response.status: number

The status read-only property of the Response interface contains the HTTP status codes of the response.

status
}`);
} return const response: Responseresponse.Body.json(): Promise<any>json(); }, });

A not-found can also be a plain value: return null and let the page decide, or throw your router's notFound() from the queryFn so the route renders its not-found component.

Streaming

A queryFn that returns an async iterable is a stream. Pass stream to fold its chunks into the data: initial is the data before the first chunk, and reduce adds a chunk to it.

export function 
function liveCommentsQuery(input: {
    slug: string;
}): QueryOptions<readonly ["posts", "liveComments", string], readonly PostComment[], readonly PostComment[], {
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}>
liveCommentsQuery
(
input: {
    slug: string;
}
input
: { slug: stringslug: string }) {
return
queryOptions<readonly ["posts", "liveComments", string], PostComment, readonly PostComment[]>(options: BaseOptions<readonly ["posts", "liveComments", string]> & Partial<{
    readonly retry: number | false | undefined;
    readonly refetchOnWindowFocus: boolean | undefined;
    readonly refetchOnReconnect: boolean | undefined;
    readonly refetchInterval: number | false | undefined;
}> & {
    readonly queryFn: (context: QueryFunctionContext<readonly ["posts", "liveComments", string], {
        readonly "~options": {
            readonly retry: (value: number | false | undefined) => number | false | undefined;
            readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
            readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
            readonly refetchInterval: (value: number | ... 1 more ... | undefined) => number | ... 1 more ... | undefined;
        };
        readonly context: {};
        readonly options: {
            ...;
        };
    }>) => AsyncIterable<...>;
    readonly stream: StreamOptions<...>;
}): QueryOptions<...> (+1 overload)
queryOptions
({
BaseOptions<readonly ["posts", "liveComments", string]>.queryKey: readonly ["posts", "liveComments", string]

A plain array, usually built by a group(). Handles keep its literal type.

queryKey
:
const posts: Group
<readonly ["liveComments", string]>(rest_0: "liveComments", rest_1: string) => readonly ["posts", "liveComments", string]
posts
("liveComments",
input: {
    slug: string;
}
input
.slug: stringslug),
queryFn: (context: QueryFunctionContext<readonly ["posts", "liveComments", string], {
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}>) => AsyncIterable<...>
queryFn
: (
context: QueryFunctionContext<readonly ["posts", "liveComments", string], {
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}>
context
) => function streamComments(slug: string, signal: AbortSignal): AsyncIterable<PostComment>streamComments(
input: {
    slug: string;
}
input
.slug: stringslug,
context: QueryFunctionContext<readonly ["posts", "liveComments", string], {
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}>
context
.signal: AbortSignal

Aborted when the request is restarted or its entry is removed.

signal
),
stream: StreamOptions<readonly PostComment[], PostComment>stream: { StreamOptions<readonly PostComment[], PostComment>.initial: readonly PostComment[]

The data before the first chunk.

initial
: [] as interface ReadonlyArray<T>ReadonlyArray<PostComment>,
StreamOptions<readonly PostComment[], PostComment>.reduce: (data: readonly PostComment[], chunk: PostComment) => readonly PostComment[]

Fold a chunk into the data.

reduce
: (comments: readonly PostComment[]comments, comment: PostCommentcomment) => [...comments: readonly PostComment[]comments, comment: PostCommentcomment],
}, }); }

Readers get the data after the first chunk, and every later chunk updates it in place. The state's isFetching stays true until the stream ends.

The query function context

Each queryFn receives a context object:

  • queryKey, the query's key, typed as the factory wrote it
  • signal: AbortSignal, aborted when the request is cancelled. Pass it to fetch so cancelled requests stop using the network.
  • client, the query client making the request

Paged queries also get:

  • pageParam, the page to load
  • direction: "forward" | "backward", which end of the list the page is added to

Context from the client's plugins

A client's plugins can add to the context. A plugin that declares a context gives its fields to every queryFn the client runs. A server makes one client per request, so this is where per-request services go, such as an API client that sends the request's cookies:

import {
	function bindQueryOptions<T = NoDecl>(): QueryOptionsBuilder<RootOf<T>, SetsOf<T>>

Makes queryOptions, the builder of a query factory's options, bound to the clients its factories run on:

bindQueryOptions
,
function createQueryClient<const TPlugins extends ReadonlyArray<AnyPlugin> = []>(config?: Omit<QueryClientConfig, "defaults" | "plugins"> & {
    readonly defaults?: NoInfer<Omit<CoreOptions, "ssr"> & Partial<RootOfPlugins<TPlugins>["options"]>>;
    readonly plugins?: TPlugins;
}): QueryClient<RootOfPlugins<TPlugins>>

Create a query client. Behaviors are opt-in: pass plugins, such as recommended(). The client's root is what they declare.

createQueryClient
,
function plugin<TDecl extends OptionsDecl = NoDecl>(definition?: PlainDefinition<NoInfer<TDecl>>): Plugin<NoInfer<TDecl>> (+2 overloads)

A plugin for a client, with what it declares: the per-query options it reads and the context it gives every queryFn. A client's queries take those options, and get that context, only when the client has the plugin.

plugin
,
function recommended(): Recommended

The behaviors most apps want: retries, refetching on focus and reconnect, and polling. Pass it as the client's plugins, or spread it among your own:

recommended
,
} from "@raf-query/react"; interface Api { readonly
Api.context: {
    readonly api: ApiClient;
}
context
: { readonly api: ApiClientapi: ApiClient };
} // On a server: called once per request, with an API client that sends the // request's cookies. export function
function makeQueryClient(apiClient: ApiClient): QueryClient<{
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {
        readonly api: ApiClient;
    };
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}>
makeQueryClient
(apiClient: ApiClientapiClient: ApiClient) {
return
createQueryClient<readonly [Plugin<Retry>, Plugin<FocusRefetch>, Plugin<ReconnectRefetch>, Plugin<Polling>, Plugin<NoInfer<Api>>]>(config?: Omit<QueryClientConfig, "defaults" | "plugins"> & {
    readonly defaults?: NoInfer<Omit<CoreOptions, "ssr"> & Partial<{
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | false | undefined;
    }>> | undefined;
    readonly plugins?: readonly [...] | undefined;
}): QueryClient<...>

Create a query client. Behaviors are opt-in: pass plugins, such as recommended(). The client's root is what they declare.

createQueryClient
({
plugins?: readonly [Plugin<Retry>, Plugin<FocusRefetch>, Plugin<ReconnectRefetch>, Plugin<Polling>, Plugin<NoInfer<Api>>] | undefinedplugins: [...function recommended(): Recommended

The behaviors most apps want: retries, refetching on focus and reconnect, and polling. Pass it as the client's plugins, or spread it among your own:

recommended
(), plugin<Api>(definition: ClientContextDefinition<NoInfer<Api>>): Plugin<NoInfer<Api>> (+2 overloads)

A plugin for a client, with what it declares: the per-query options it reads and the context it gives every queryFn. A client's queries take those options, and get that context, only when the client has the plugin.

plugin
<Api>({
ClientContextDefinition<NoInfer<Api>>.context: {
    readonly api: ApiClient;
}

Merged into the context of every queryFn the client runs.

context
: { api: ApiClientapi: apiClient: ApiClientapiClient } })],
}); } // In the app's `raf.ts` const
const queryOptions: QueryOptionsBuilder<{
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {
        readonly api: ApiClient;
    };
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}, {
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {
        readonly api: ApiClient;
    };
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}>
queryOptions
=
bindQueryOptions<(apiClient: ApiClient) => QueryClient<{
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {
        readonly api: ApiClient;
    };
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}>>(): QueryOptionsBuilder<...>

Makes queryOptions, the builder of a query factory's options, bound to the clients its factories run on:

bindQueryOptions
<typeof
function makeQueryClient(apiClient: ApiClient): QueryClient<{
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {
        readonly api: ApiClient;
    };
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}>
makeQueryClient
>();
export function
function postQuery(input: {
    slug: string;
}): QueryOptions<readonly ["posts", "bySlug", string], Post, Post, {
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {
        readonly api: ApiClient;
    };
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}>
postQuery
(
input: {
    slug: string;
}
input
: { slug: stringslug: string }) {
return
const queryOptions: QueryOptionsBuilder
<readonly ["posts", "bySlug", string], Post, Post>(options: BaseOptions<readonly ["posts", "bySlug", string]> & Partial<{
    readonly retry: number | false | undefined;
    readonly refetchOnWindowFocus: boolean | undefined;
    readonly refetchOnReconnect: boolean | undefined;
    readonly refetchInterval: number | false | undefined;
}> & {
    readonly queryFn: (context: QueryFunctionContext<readonly ["posts", "bySlug", string], {
        readonly "~options": {
            readonly retry: (value: number | false | undefined) => number | false | undefined;
            readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
            readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
            readonly refetchInterval: (value: number | ... 1 more ... | undefined) => number | ... 1 more ... | undefined;
        };
        readonly context: {
            ...;
        };
        readonly options: {
            ...;
        };
    }>) => Awaitable<...>;
    readonly select?: ((data: Post) => Post) | undefined;
}) => QueryOptions<...> (+1 overload)
queryOptions
({
BaseOptions<readonly ["posts", "bySlug", string]>.queryKey: readonly ["posts", "bySlug", string]

A plain array, usually built by a group(). Handles keep its literal type.

queryKey
: posts<readonly ["bySlug", string]>(rest_0: "bySlug", rest_1: string): readonly ["posts", "bySlug", string]posts("bySlug",
input: {
    slug: string;
}
input
.slug: stringslug),
queryFn: (context: QueryFunctionContext<readonly ["posts", "bySlug", string], {
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {
        readonly api: ApiClient;
    };
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}>) => Awaitable<...>
queryFn
: ({ api: ApiClientapi, signal: AbortSignal

Aborted when the request is restarted or its entry is removed.

signal
}) => api: ApiClientapi.
ApiClient.getPost(slug: string, init: {
    signal: AbortSignal;
}): Promise<Post>
getPost
(
input: {
    slug: string;
}
input
.slug: stringslug, { signal: AbortSignalsignal }),
}); }

A plugin can give its context per request instead, from its link.

api is typed because this queryOptions is bound to the client that has the plugin. A queryFn built for a client without it gets queryKey, signal and client only.

Redirects

In TanStack Start, a queryFn can throw redirect({ to: "/login" }). The router integration turns it into a navigation, from a loader or from a background refetch. Pass handleRedirects: false to setupRouterIntegration to handle them yourself.