Use HyperFetch with TanStack Query and SWR
TanStack Query and SWR cache the result of any async function, and a typed HyperFetch request is a good one to give
them. Let the cache library own caching and retries, and let HyperFetch send the request with exec(). This guide
covers queries, mutations, cancellation, errors and keys.
- Why
exec()is the method to call from a query function. - How to pass the
signalof TanStack Query so a cancelled query cancels its request. - How
throwOnErrorturns failed responses into rejected promises, per call, per request or for the whole API. - How to write mutations and pick query keys.
- What HyperFetch features you keep and give up in this setup.
When to use it?
- You already use TanStack Query or SWR and want typed requests without rewriting the data layer.
- You move to HyperFetch screen by screen and both libraries run side by side for a while.
- You want HyperFetch adapters (GraphQL, Firebase, Axios) behind a TanStack Query cache.
If you start fresh, the HyperFetch hooks do the same job without a second library.
Why exec() and not send()?
send() goes through the HyperFetch dispatcher and cache: it deduplicates, retries and stores the response. TanStack
Query and SWR do all of that themselves, so with send() you get two caches and two layers of retries. exec() sends
the request straight to the adapter. Interceptors, auth, the response mapper and the lifecycle callbacks still run.
| Feature | Owned by |
|---|---|
| Cache, stale time, refetching | TanStack Query / SWR |
| Deduplication and retries | TanStack Query / SWR |
| Endpoint, params, payload and types | HyperFetch request |
| Auth, headers, interceptors | HyperFetch client |
| Response mapping and validation | HyperFetch request |
| Transport (HTTP, GraphQL, Firebase) | HyperFetch adapter |
Queries
TanStack Query passes an AbortSignal to every query function and tells failures by a rejected promise. Pass both to
exec():
import { useQuery } from "@tanstack/react-query";
const getUser = client.createRequest<{ response: User }>()({
endpoint: "/users/:userId",
});
export const useUser = (userId: number) => {
const request = getUser.setParams({ userId });
return useQuery({
queryKey: ["users", userId],
queryFn: async ({ signal }) => {
const { data } = await request.exec({ signal, throwOnError: true });
return data;
},
});
};
signalcancels only this call when TanStack Query cancels the query - when the component unmounts or the key changes. Other calls of the same request keep running.throwOnErrorrejects with theerrorof a failed response, so the query goes to its error state. A successful response resolves the same way as without it, which is why the function returnsdata.
Setting throwOnError once
Writing throwOnError: true in every query function gets repetitive. It is a regular request option, so you can set it
where the request is defined, or for many requests at once. A value given to the call always wins.
// On one request
const getUser = client.createRequest<{ response: User }>()({
endpoint: "/users/:userId",
throwOnError: true,
});
// On every request of an SDK
const api = createSdk<typeof client, Schema>(client).$configure({
"*": { throwOnError: true },
});
// On every request of the client
client.adapter.setRequestDefaults((options) => ({
// This replaces the adapter's defaults,
// so keep deduplication of the reads
deduplicate: (options.method ?? "GET") === "GET",
throwOnError: true,
}));
The React hooks of HyperFetch ignore it - useFetch, useSubmit, useInfiniteFetch and useStream keep reporting
errors through their state. So one client can serve TanStack Query and the HyperFetch hooks at the same time.
Mutations
Mutations work the same way. Pass the payload to exec() and invalidate the queries the mutation changed.
import { useMutation, useQueryClient } from "@tanstack/react-query";
const updateUser = client.createRequest<{
response: User;
payload: Partial<User>;
}>()({
endpoint: "/users/:userId",
method: "PATCH",
throwOnError: true,
});
export const useUpdateUser = (userId: number) => {
const queryClient = useQueryClient();
return useMutation({
mutationFn: async (payload: Partial<User>) => {
const { data } = await updateUser.setParams({ userId }).exec({ payload });
return data;
},
onSuccess: () =>
queryClient.invalidateQueries({ queryKey: ["users", userId] }),
});
};
SWR
SWR calls the fetcher with the key and tells failures by a rejected promise. It does not pass a signal.
import useSWR from "swr";
const getUsers = client.createRequest<{ response: User[] }>()({
endpoint: "/users",
throwOnError: true,
});
export const useUsers = () =>
useSWR(getUsers.cacheKey, async () => {
const { data } = await getUsers.exec();
return data;
});
Picking query keys
Keys belong to the cache library, so pick the ones that fit its invalidation. Arrays like ["users", userId] let
TanStack Query invalidate all users at once. When you only need a stable key, request.cacheKey is one: it is built
from the method, the endpoint with its params and the query params.
What you give up
With TanStack Query or SWR owning the cache, the HyperFetch features that live in the dispatcher and the cache do not
apply to these calls: request queues, offline replay, HyperFetch retries and deduplication, optimistic updates of
setOptimistic, cache invalidation with client.cache and the cache view of the devtools. Use the counterparts of the
cache library instead. Interceptors, auth, mappers, validation, upload and download progress and the request lifecycle
callbacks keep working.
The two caches don't share data. Read a given resource through one of them - mixing useFetch and useQuery for the
same endpoint shows different data on different screens.
You can now use HyperFetch requests inside TanStack Query and SWR.
- You call
exec()from query functions, so the cache library owns caching and retries. - You pass the
signalof the query, so a cancelled query cancels only its own request. - You set
throwOnErrorper call, per request, in the SDK or for the whole client. - You write mutations with
exec({ payload })and invalidate queries after them. - You know which HyperFetch features stay and which ones the cache library takes over.