@bunvex/react-query
v0.1.0-alpha.1
Published
TanStack Query integration: live bunvex queries in the QueryClient cache, server rendering included.
Readme
@bunvex/react-query
TanStack Query integration (STUDY-55), the counterpart of Convex's @convex-dev/react-query. Each cached
bunvexQuery is one live subscription: new results are pushed into the QueryClient's cache (no refetch, so
staleTime is Infinity), and the subscription is dropped gcTime after the last useQuery using it unmounts.
import { anyApi as api } from "@bunvex/client";
import { BunvexProvider, BunvexReactClient } from "@bunvex/react";
import { BunvexQueryClient, bunvexQuery, useBunvexMutation } from "@bunvex/react-query";
import { QueryClient, QueryClientProvider, useMutation, useQuery } from "@tanstack/react-query";
const bunvex = new BunvexReactClient(import.meta.env.VITE_BUNVEX_URL);
const bunvexQueryClient = new BunvexQueryClient(bunvex);
const queryClient = new QueryClient({
defaultOptions: { queries: { queryKeyHashFn: bunvexQueryClient.hashFn(), queryFn: bunvexQueryClient.queryFn() } },
});
bunvexQueryClient.connect(queryClient);
function Messages() {
const { data } = useQuery({ ...bunvexQuery(api.messages.list, {}), gcTime: 10_000 });
const { mutate } = useMutation({ mutationFn: useBunvexMutation(api.messages.send) });
return <button onClick={() => mutate({ body: "hi" })}>{data?.length ?? "…"}</button>;
}
export const App = () => (
<BunvexProvider client={bunvex}>
<QueryClientProvider client={queryClient}>
<Messages />
</QueryClientProvider>
</BunvexProvider>
);bunvexQuery(fn, args | "skip"),useSuspenseQuerytoo;bunvexAction(fn, args)runs an action as a (non-live) query.- Server rendering (Next.js App Router, TanStack Start): without
window,queryFnreads over HTTP, every query of the render at one snapshot (dangerouslyUseInconsistentQueriesDuringSSRreads each at the latest);prefetchQuery+dehydrateon the server,HydrationBoundaryon the client.serverFetchsets the fetch. - Args are JSON-encoded in the key, so bigints and bytes survive
dehydrateand persisters. - The bunvex hooks are re-exported as
useBunvexQuery,useBunvexMutation,useBunvexAction, … so they never clash with TanStack's.
