Guides
Query invalidation
Mark queries stale after a write, refetch the ones on screen, and select queries by factory, group or predicate, such as tags.
Waiting for data to go stale on its own doesn't work after a write: the user just changed something and expects to see it. invalidateOne and invalidateMany mark queries stale and refetch the ones that are being read:
// One query: a factory and its input
await invalidateOne<(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 options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}>>(client: ClientFor<...>, factory: (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 options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}>, input: {
...;
}): Promise<void>
Mark one query stale: a factory and its full input. If it's read, it
refetches; resolves when that settles.
invalidateOne(const qc: 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 tags: (value: readonly string[] | undefined) => readonly string[] | undefined;
};
readonly context: {};
readonly options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
readonly tags: readonly string[] | undefined;
};
}>
qc, 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 options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}>
postQuery, { slug: stringslug });
// Every query whose key starts with the `posts` group
await function invalidateMany(client: QueryClient, group: Group): Promise<void> (+3 overloads)Mark queries stale: a factory's (optionally only those whose input
matches partial), a group's (keys that start with its path, whether
the group built them or not), or those a target's where picks. Read
queries refetch now; the rest when next read. Resolves when the
refetches settle.
invalidateMany(const qc: 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 tags: (value: readonly string[] | undefined) => readonly string[] | undefined;
};
readonly context: {};
readonly options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
readonly tags: readonly string[] | undefined;
};
}>
qc, const posts: Group<readonly ["posts"]>posts);
When a query is invalidated:
- It's marked stale, whatever its
staleTime. The nextloadthat starts it refetches it. - If something is reading it, it refetches now, in the background. Readers keep showing the current data and re-render when the new data lands.
- If nothing is reading it, nothing happens until the next time something starts or reads it.
The verbs return a promise that resolves once every refetch they started has settled. Await it at the end of an action, and the action stays pending until the fresh data is on screen.
Try it. Each list below reads its own query. Invalidate a group, a factory, or one query, and watch which ones refetch:
Targets
Verbs are functions that take the client first. Each …One verb takes a factory and its full input; each …Many verb takes a target:
| Call | Matches |
|---|---|
invalidateOne(qc, factory, input) | The factory's query for that input |
invalidateMany(qc, factory) | Every query of the factory |
invalidateMany(qc, factory, partialInput) | The factory's queries whose input matches partialInput |
invalidateMany(qc, group) | Every key that starts with the group, or a group in it |
invalidateMany(qc, { where }, param?) | Every query where returns true for |
invalidateMany(qc, [target, …]) | Every query any of the targets matches: several targets |
invalidateAll(qc) | Everything |
A partial input matches the input each query's factory was called with, never its key, so it works the same however the key is built: [postQuery, { slug }] for a key written as posts("bySlug", input.slug). Object fields match partially: { sort: "newest" } matches any input with that field, whatever else it has.
A query that was only hydrated has no factory until something reads it. A factory target still invalidates it, from its first read. resetMany and removeMany act at once, so a factory target skips it; a group reaches it by its key.
Targets are values, never strings: a factory matches by identity, a group by its path, and a where target, such as a tag, by what it says about each query. TypeScript checks a partial input against the factory's input, and a target's parameter against the target.
Several targets
To invalidate more than one thing, pass a list. An element is a group, a factory, a target, or [factory, partialInput] / [target, param]:
// One call: the refetches run side by side, and a query that two targets
// cover refetches once
await invalidateMany<{
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 tags: (value: readonly string[] | undefined) => readonly string[] | undefined;
};
readonly context: {};
readonly options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
readonly tags: readonly string[] | undefined;
};
}, readonly [...]>(client: QueryClient<...>, targets: readonly [...]): Promise<void> (+3 overloads)
Mark queries stale: a factory's (optionally only those whose input
matches partial), a group's (keys that start with its path, whether
the group built them or not), or those a target's where picks. Read
queries refetch now; the rest when next read. Resolves when the
refetches settle.
invalidateMany(const qc: 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 tags: (value: readonly string[] | undefined) => readonly string[] | undefined;
};
readonly context: {};
readonly options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
readonly tags: readonly string[] | undefined;
};
}>
qc, [
const authors: Group<readonly ["authors"]>authors, // a group
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 options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}>
postQuery, // a factory: all its queries
[function postsQuery(input: {
page: number;
sort: "newest" | "oldest";
}): QueryOptions<readonly ["posts", "list", {
page: number;
sort: "newest" | "oldest";
}], readonly Post[], readonly 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 options: {
...;
};
}>
postsQuery, { sort?: "newest" | "oldest" | undefinedsort: "newest" }], // a factory with a partial input
[const tags: Target<Tagged, string>tags, `post:${const slug: stringslug}`], // a target with its parameter
]);
The refetches run side by side, a query that several targets cover refetches once, and the promise resolves when all of them have settled. Awaiting one verb after another serializes them instead: await invalidateMany(qc, a); await invalidateMany(qc, b); starts b's refetches only once a's are done. TypeScript checks each element on its own, the way it checks a single target.
Matching with where
A target is any object with where(query, param), typed Target<TOptions, TParam>. Its where gets each query's queryKey, input, options and data (before select), and the parameter the verb was called with:
import type { interface Target<TOptions = object, TParam = never>Picks out queries by what they are: where gets each query and the
parameter the verb was called with.
Target } from "@raf-query/react";
// Every query whose key mentions a post's slug
const const mentionsPost: Target<object, string>mentionsPost: interface Target<TOptions = object, TParam = never>Picks out queries by what they are: where gets each query and the
parameter the verb was called with.
Target<object, string> = {
Target<object, string>.where: (query: QueryInfo<object>, param: string) => booleanwhere: (query: QueryInfo<object>query, postSlug: stringpostSlug) => query: QueryInfo<object>query.QueryInfo<object>.queryKey: QueryKeyqueryKey.ReadonlyArray<unknown>.includes(searchElement: unknown, fromIndex?: number): booleanDetermines whether an array includes a certain element, returning true or false as appropriate.
includes(postSlug: stringpostSlug),
};
await invalidateMany<{
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 tags: (value: readonly string[] | undefined) => readonly string[] | undefined;
};
readonly context: {};
readonly options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
readonly tags: readonly string[] | undefined;
};
}, string>(client: QueryClient<...>, target: TargetFor<...>, param: string): Promise<void> (+3 overloads)
Mark queries stale: a factory's (optionally only those whose input
matches partial), a group's (keys that start with its path, whether
the group built them or not), or those a target's where picks. Read
queries refetch now; the rest when next read. Resolves when the
refetches settle.
invalidateMany(const qc: 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 tags: (value: readonly string[] | undefined) => readonly string[] | undefined;
};
readonly context: {};
readonly options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
readonly tags: readonly string[] | undefined;
};
}>
qc, const mentionsPost: Target<object, string>mentionsPost, const slug: stringslug);
// Every query that has no data yet
await invalidateMany<{
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 tags: (value: readonly string[] | undefined) => readonly string[] | undefined;
};
readonly context: {};
readonly options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
readonly tags: readonly string[] | undefined;
};
}, never>(client: QueryClient<...>, target: TargetFor<...>): Promise<void> (+3 overloads)
Mark queries stale: a factory's (optionally only those whose input
matches partial), a group's (keys that start with its path, whether
the group built them or not), or those a target's where picks. Read
queries refetch now; the rest when next read. Resolves when the
refetches settle.
invalidateMany(const qc: 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 tags: (value: readonly string[] | undefined) => readonly string[] | undefined;
};
readonly context: {};
readonly options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
readonly tags: readonly string[] | undefined;
};
}>
qc, { Target<NoInfer<{ readonly retry: number | false | undefined; readonly refetchOnWindowFocus: boolean | undefined; readonly refetchOnReconnect: boolean | undefined; readonly refetchInterval: number | ... 1 more ... | undefined; readonly tags: readonly string[] | undefined; }>, never>.where: (query: QueryInfo<NoInfer<{
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | false | undefined;
readonly tags: readonly string[] | undefined;
}>>, param: never) => boolean
where: (query: QueryInfo<NoInfer<{
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | false | undefined;
readonly tags: readonly string[] | undefined;
}>>
query) => query: QueryInfo<NoInfer<{
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | false | undefined;
readonly tags: readonly string[] | undefined;
}>>
query.QueryInfo<TOptions = object>.data: unknownThe query's data, before select; undefined without data.
data === var undefinedundefined });input is the input of the latest factory call for that entry. An entry that was only hydrated, and not yet read or started on this client, has undefined input and {} as its options, so match on queryKey to include those. TOptions is the options the target reads; object fits a target that reads none. query.options has the options the client's plugins declare: see Selecting queries by options.
Tags
A tag cuts across groups: every query that shows some content, wherever its key starts. raf-query has no tags of its own, because a tag is a where target over an option: a few lines in your app. A plugin that only declares a tags option gives it to the client's queries, and tag(name) matches the queries that list name:
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 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 } from "@raf-query/react";
interface Tags {
readonly Tags.options: {
readonly tags?: ReadonlyArray<string>;
}
options: { readonly tags?: readonly string[] | undefinedtags?: interface ReadonlyArray<T>ReadonlyArray<string> };
}
// Declares the `tags` option on the clients that have it
const const tagged: Plugin<NoInfer<Tags>>tagged = plugin<Tags>(definition?: PlainDefinition<NoInfer<Tags>> | undefined): Plugin<NoInfer<Tags>> (+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<Tags>();
export function function makeQueryClient(): QueryClient<{
readonly "~options": {
readonly tags: (value: readonly string[] | undefined) => readonly string[] | undefined;
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 tags: readonly string[] | undefined;
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}>
makeQueryClient() {
return createQueryClient<readonly [Plugin<Retry>, Plugin<FocusRefetch>, Plugin<ReconnectRefetch>, Plugin<Polling>, Plugin<NoInfer<Tags>>]>(config?: Omit<QueryClientConfig, "defaults" | "plugins"> & {
readonly defaults?: NoInfer<Omit<CoreOptions, "ssr"> & Partial<{
readonly tags: readonly string[] | undefined;
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | 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<Tags>>] | undefinedplugins: [...function recommended(): RecommendedThe 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(), const tagged: Plugin<NoInfer<Tags>>tagged] });
}
// In the app's `raf.ts`
const const queryOptions: QueryOptionsBuilder<{
readonly "~options": {
readonly tags: (value: readonly string[] | undefined) => readonly string[] | undefined;
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 tags: readonly string[] | undefined;
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}, {
readonly "~options": {
readonly tags: (value: readonly string[] | undefined) => readonly string[] | undefined;
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 tags: readonly string[] | undefined;
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}>
queryOptions = bindQueryOptions<() => QueryClient<{
readonly "~options": {
readonly tags: (value: readonly string[] | undefined) => readonly string[] | undefined;
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 tags: readonly string[] | undefined;
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(): QueryClient<{
readonly "~options": {
readonly tags: (value: readonly string[] | undefined) => readonly string[] | undefined;
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 tags: readonly string[] | undefined;
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}>
makeQueryClient>();
export function function postBySlugQuery(input: {
slug: string;
}): QueryOptions<readonly ["posts", "bySlug", string], Post, Post, {
readonly "~options": {
readonly tags: (value: readonly string[] | undefined) => readonly string[] | undefined;
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: {
...;
};
}>
postBySlugQuery(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 tags: readonly string[] | undefined;
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 tags: (value: readonly string[] | undefined) => readonly string[] | undefined;
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: {
...;
};
}>) => 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 tags: (value: readonly string[] | undefined) => readonly string[] | undefined;
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 tags: readonly string[] | undefined;
readonly retry: number | ... 1 more ... | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}>) => Awaitable<...>
queryFn: () => function getPost(slug: string): Promise<Post>getPost(input: {
slug: string;
}
input.slug: stringslug),
tags?: readonly string[] | undefinedtags: ["content", `post:${input: {
slug: string;
}
input.slug: stringslug}`],
});
}
// The option the way a client with `tagged` has it
type type Tagged = {
readonly tags: ReadonlyArray<string> | undefined;
}
Tagged = { readonly tags: readonly string[] | undefinedtags: interface ReadonlyArray<T>ReadonlyArray<string> | undefined };
// Every query that lists the tag
const const tag: (name: string) => Target<Tagged>tag = (name: stringname: string): interface Target<TOptions = object, TParam = never>Picks out queries by what they are: where gets each query and the
parameter the verb was called with.
Target<type Tagged = {
readonly tags: ReadonlyArray<string> | undefined;
}
Tagged> => ({
Target<Tagged, never>.where: (query: QueryInfo<Tagged>, param: never) => booleanwhere: (query: QueryInfo<Tagged>query) => query: QueryInfo<Tagged>query.QueryInfo<Tagged>.options: TaggedThe options its factory gave it. An entry only hydrated has {} until it's read.
options.tags: readonly string[] | undefinedtags?.ReadonlyArray<string>.includes(searchElement: string, fromIndex?: number): booleanDetermines whether an array includes a certain element, returning true or false as appropriate.
includes(name: stringname) ?? false,
});
const const qc: QueryClient<{
readonly "~options": {
readonly tags: (value: readonly string[] | undefined) => readonly string[] | undefined;
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 tags: readonly string[] | undefined;
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}>
qc = function makeQueryClient(): QueryClient<{
readonly "~options": {
readonly tags: (value: readonly string[] | undefined) => readonly string[] | undefined;
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 tags: readonly string[] | undefined;
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}>
makeQueryClient();
await invalidateMany<{
readonly "~options": {
readonly tags: (value: readonly string[] | undefined) => readonly string[] | undefined;
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 tags: readonly string[] | undefined;
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}, never>(client: QueryClient<...>, target: TargetFor<...>): Promise<void> (+3 overloads)
Mark queries stale: a factory's (optionally only those whose input
matches partial), a group's (keys that start with its path, whether
the group built them or not), or those a target's where picks. Read
queries refetch now; the rest when next read. Resolves when the
refetches settle.
invalidateMany(const qc: QueryClient<{
readonly "~options": {
readonly tags: (value: readonly string[] | undefined) => readonly string[] | undefined;
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 tags: readonly string[] | undefined;
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}>
qc, const tag: (name: string) => Target<Tagged>tag("content"));
Tagged names the option the way a client with the plugin has it: present, and possibly undefined. Typed that way, tag() fits only clients made with tagged, and TypeScript rejects it on the others. To have one target for every tag, take the tag as the parameter:
// One target for every tag: the tag is its parameter
const const tags: Target<Tagged, string>tags: interface Target<TOptions = object, TParam = never>Picks out queries by what they are: where gets each query and the
parameter the verb was called with.
Target<type Tagged = {
readonly tags: ReadonlyArray<string> | undefined;
}
Tagged, string> = {
Target<Tagged, string>.where: (query: QueryInfo<Tagged>, param: string) => booleanwhere: (query: QueryInfo<Tagged>query, name: stringname) => query: QueryInfo<Tagged>query.QueryInfo<Tagged>.options: TaggedThe options its factory gave it. An entry only hydrated has {} until it's read.
options.tags: readonly string[] | undefinedtags?.ReadonlyArray<string>.includes(searchElement: string, fromIndex?: number): booleanDetermines whether an array includes a certain element, returning true or false as appropriate.
includes(name: stringname) ?? false,
};
await invalidateMany<{
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 tags: (value: readonly string[] | undefined) => readonly string[] | undefined;
};
readonly context: {};
readonly options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
readonly tags: readonly string[] | undefined;
};
}, string>(client: QueryClient<...>, target: TargetFor<...>, param: string): Promise<void> (+3 overloads)
Mark queries stale: a factory's (optionally only those whose input
matches partial), a group's (keys that start with its path, whether
the group built them or not), or those a target's where picks. Read
queries refetch now; the rest when next read. Resolves when the
refetches settle.
invalidateMany(const qc: 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 tags: (value: readonly string[] | undefined) => readonly string[] | undefined;
};
readonly context: {};
readonly options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
readonly tags: readonly string[] | undefined;
};
}>
qc, const tags: Target<Tagged, string>tags, `post:${const slug: stringslug}`);
To pick queries by their data instead, read query.data in where. It's undefined until a query has data.
Invalidate, reset or remove
| Verbs | Data | Read queries | Use for |
|---|---|---|---|
invalidate… | Kept, shown meanwhile | Refetch in the background | After writes: the normal case |
reset… | Dropped | Suspend and refetch | Data that must not be shown again (logout) |
remove… | Dropped with the entry | Keep what they rendered until they re-render | Freeing memory, dropping an abandoned key |
Each comes as …One, …Many and …All, with the same targets; resetMany and removeMany take a list too. reset resolves when the refetches it caused settle, like invalidate. remove is synchronous, and aborts in-flight requests.
Invalidating from mutations
The common pattern is to invalidate at the end of a write, so it covers whatever the write affected. Await a verb at the end of the action:
<React.JSX.IntrinsicElements.form: React.DetailedHTMLProps<React.FormHTMLAttributes<HTMLFormElement>, HTMLFormElement>form
React.FormHTMLAttributes<HTMLFormElement>.action?: string | ((formData: FormData) => void | Promise<void>) | undefinedaction={async (formData: FormDataformData) => {
await function addComment(input: {
body: string;
slug: string;
}): Promise<PostComment>
addComment({
body: stringbody: var String: StringConstructor
(value?: any) => string
Allows manipulation and formatting of text strings and determination and location of substrings within strings.
String(formData: FormDataformData.FormData.get(name: string): FormDataEntryValue | nullThe get() method of the FormData interface returns the first value associated with a given key from within a FormData object. If you expect multiple values and want all of them, use the getAll() method instead.
get("body")),
slug: stringslug: props: {
slug: string;
}
props.slug: stringslug,
});
await function invalidateMany(client: QueryClient, group: Group): Promise<void> (+3 overloads)Mark queries stale: a factory's (optionally only those whose input
matches partial), a group's (keys that start with its path, whether
the group built them or not), or those a target's where picks. Read
queries refetch now; the rest when next read. Resolves when the
refetches settle.
invalidateMany(const qc: 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 options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}>
qc, const comments: Group<readonly ["posts", "comments"]>comments);
}}
>
{/* ... */}
</React.JSX.IntrinsicElements.form: React.DetailedHTMLProps<React.FormHTMLAttributes<HTMLFormElement>, HTMLFormElement>form>
);
}
When a write changes more than one thing, invalidate them in one call, so their refetches run side by side. Prefer invalidating a little too much over too little. Only queries being read refetch, so a broad group costs a request per list on screen, not per list ever loaded.
When the server's response has everything the cache needs, write it with setData instead and skip the refetch. See Mutations.
Invalidation on the server
A server render's client never revalidates cached data, because it renders once: prefetch and swr use what the cache has, even after an invalidation. A revalidate refetches an invalidated entry, so it sees the write. This matters in Next.js server actions that render again after a write.