
Security News
CVE Volume Surges Past 48,000 in 2025 as WordPress Plugin Ecosystem Drives Growth
CVE disclosures hit a record 48,185 in 2025, driven largely by vulnerabilities in third-party WordPress plugins.
❗ This package is no longer being actively maintained. ❗
Batteries included fetching library Fetch your data with ease and give your users a better experience
The most simple query is a parameter without parameters, it's just a wrapper around and Observable.
The query method expects a callback method to invoke the query.
import { query } from "rx-query";
characters$ = query("characters", () =>
this.rickAndMortyService.getCharacters(),
);
A query that has a static parameter (a value that doesn't change over time), can be written in the same way as a query without parameters.
import { query } from "rx-query";
characters$ = query("character", () =>
this.rickAndMortyService.getCharacter(1),
);
An alternative way if to pass the static parameter as the first argument. The query callback will then be invoked with the passed parameter.
import { query } from "rx-query";
characters$ = query("character", 1, (characterId) =>
this.rickAndMortyService.getCharacter(characterId),
);
If a parameter can change over time (aka an Observable), it can also be passed as a parameter to query.
When the input Observable emits a new value, the callback query will be invoked with the new input value.
character$ = query(
"character",
this.activatedRoute.params.pipe(map((p) => p.characterId)),
(characterId: number) => this.rickAndMortyService.getCharacter(characterId),
);
A query can have the following:
loading: when the query is being invoked and hasn't responded yetrefreshing: when the query is being invoked, and there's a cached value (the cached value gets refreshed when the query is successful)success: when the query returns a successful responseerror: when the query threw an errormutating: when a mutation is in progressmutate-error: when a mutation threw an errorIn the view layer you will often see a structure like this, with a segment to represent each status:
<ng-container *ngIf="characters$ | async as characters">
<ng-container [ngSwitch]="characters.status">
<div *ngSwitchCase="'loading'">Loading ... ({{ characters.retries }})</div>
<div *ngSwitchCase="'error'">
Something went wrong ... ({{ characters.retries }})
</div>
<div *ngSwitchDefault>
<ul>
<li *ngFor="let character of characters.data">
<a [routerLink]="character.id">{{ character.name }}</a>
</li>
</ul>
</div>
</ng-container>
</ng-container>
Use refreshQuery to trigger a new fetch from a previously contructed query.
Note that the key and parameters provided to refreshQuery should be exactly the same!
The following will refetch the data and update the cache.
import { query, refreshQuery } from "rx-query";
character$ = query("character", 1, (id) =>
this.rickAndMortyService.getCharacter(id),
);
// On some event
refreshQuery("character", 1);
export type QueryOutput<QueryResult = unknown> = {
status: Readonly<
| "idle"
| "success"
| "error"
| "loading"
| "refreshing"
| "mutating"
| "mutate-error"
>;
data?: Readonly<QueryResult>;
error?: Readonly<unknown>;
retries?: Readonly<number>;
mutate: (data: QueryResult) => void;
};
statusThe current status of the query.
dataThe result of the query, or the cached result.
errorThe error object returned by the query. Only available in the error status.
retriesNumber of query retries. Is reset every time data is fetched. Available on all statuses.
mutateThe mutate method to mutate the current query. This is optimistic, the data of the query will be modified while the request is pending. When the request resolves, the query data will be refreshed with the server data. If the request fails, the original data of the query will be restored.
export type QueryConfig = {
retries?: number | ((retryAttempt: number, error: unknown) => boolean);
retryDelay?: number | ((retryAttempt: number) => number);
refetchInterval?: number | Observable<unknown>;
refetchOnWindowFocus?: boolean;
refetchOnReconnect?: boolean;
staleTime?: number;
cacheTime?: number;
mutator?: (data: QueryResult, params: QueryParam) => QueryResult;
};
retriesThe number of retries to retry a query before ending up in the error status.
Also accepts a callback method ((retryAttempt: number, error: unknown) => boolean) to give more control to the consumer.
When a query is being retried, the status remains in the original (loading or refreshing) status.
Example.
Default: 3
Usage:
{
retries: 3,
}
{
// Never retry when 3 attempts has been made already, or when the query is totally broken
retries: (retryAttempt: number, error: string) =>
retryAttempt < 3 && !error !== "Totally broken",
}
retryDelayThe delay in milliseconds before retrying the query.
Also accepts a callback method ((retryAttempt: number) => number) to give more control to the consumer.
Example.
Default: (n) => (n + 1) * 1000
Usage:
{
retryDelay: 100,
}
{
// Increase the delay with 1 second after every attempt
retryDelay: (retryAttempt) => retryAttempt * 1000,
}
refetchIntervalInvoke the query in the background every x milliseconds, and emit the new value when the query is resolved. Example.
Default: Infinity
Usage:
{
// every 5 minutes
refetchInterval: 6000 * 5,
}
refetchOnWindowFocusInvoke the query in the background when the window is focused, and emit the new value when the query is resolved. Example.
Default: true
Usage:
{
refetchOnWindowFocus: false,
}
refetchOnReconnectInvoke the query when the client goes back online.
Default: true
Usage:
{
refetchOnReconnect: false,
}
cacheTimeSet the cache time (in milliseconds) for a query key. Example.
Default: 30_000 (5 minutes)
Usage:
{
cacheTime: 60_000,
}
staleTimeDecides when a query should be refetched when it receives a trigger.
Default: 0
Usage:
{
staleTime: 60_000,
}
mutatorThe mutator, is the method that will be invoked when the mutate method is called.
It receives the data passed to the mutate method and the current params of the query.
Example.
Default: mutator: (data) => data
Usage:
{
mutator: (data, queryOptions) =>
this.http
.post(`/persons/${queryOptions.queryParameters.id}`, data)
// 👇 important to let the request throw in order to rollback
.pipe(catchError((err) => throwError(err.statusText))),
}
To override the defaults for all queries, you can use the setQueryConfig method.
setQueryConfig({
refetchOnWindowFocus: false,
retries: 0,
cacheTime: 60_000,
});
This library is inspired by:
FAQs
rx-query
We found that rx-query demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
CVE disclosures hit a record 48,185 in 2025, driven largely by vulnerabilities in third-party WordPress plugins.

Security News
Socket CEO Feross Aboukhadijeh joins Insecure Agents to discuss CVE remediation and why supply chain attacks require a different security approach.

Security News
Tailwind Labs laid off 75% of its engineering team after revenue dropped 80%, as LLMs redirect traffic away from documentation where developers discover paid products.