TanStack Query
TanStack Query caches the result of any async function, and a typed HyperFetch request is a good one to give it.
TanStack Query owns the cache, retries and refetching. HyperFetch sends the request with exec(), with your client's
auth, interceptors, mappers and adapter. There is no package to install beyond @hyper-fetch/core and TanStack Query.
- Typed query functions: Params, payload, response and error types come from the request.
- Real cancellation: The query's
signalaborts only its own call when TanStack Query cancels the query. - Errors TanStack Query understands:
throwOnErrorrejects with theerrorof a failed response. - One cache:
exec()skips the HyperFetch dispatcher and cache, so nothing is cached or retried twice.
Queries
Pass the signal TanStack Query gives every query function, and throwOnError so a failed response puts the query in
its error state:
import { useQuery } from "@tanstack/react-query";
const getUser = client.createRequest<{ response: User }>()({
endpoint: "/users/:userId",
});
export const useUser = (userId: number) =>
useQuery({
queryKey: ["users", userId],
queryFn: async ({ signal }) => {
const { data } = await getUser
.setParams({ userId })
.exec({ signal, throwOnError: true });
return data;
},
});
send() would go through the HyperFetch dispatcher and cache as well, which TanStack Query already does. Use exec()
in query functions.
The query function only uses @hyper-fetch/core, so the same code works in Vue Query, Solid Query, Svelte Query and
Angular Query.
Set throwOnError once
throwOnError is a request option, so set it where the request is defined, or for a whole SDK, instead of in every
query function. A value given to the call wins.
const getUser = client.createRequest<{ response: User }>()({
endpoint: "/users/:userId",
throwOnError: true,
});
const api = createSdk<typeof client, Schema>(client).$configure({
"*": { throwOnError: true },
});
The HyperFetch React hooks ignore the option, so one client can serve TanStack Query and useFetch side by side.
Mutations
Send the payload with 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] }),
});
};
- You already use TanStack Query and want typed requests without rewriting the data layer.
- You move to HyperFetch screen by screen, with both libraries running side by side.
- You want HyperFetch adapters (GraphQL, Firebase, Axios) or a generated SDK behind a TanStack Query cache.
TanStack Query and the HyperFetch cache don't share data. Read a given endpoint through one of them, or different screens show different data.
- Query functions call
exec(), so TanStack Query owns caching, retries and refetching. - The query's
signalcancels only its own call, andthrowOnErrorturns failed responses into rejections. - Set
throwOnErrorper call, per request or for a whole SDK.