TanStack Query utilities for flat Effect RPC clients. Provides oRPC-style queryOptions, mutationOptions, infiniteOptions, cursorInfiniteOptions, streamedOptions, liveOptions, hierarchical query keys, skipToken, and AbortSignal wiring.
| Need | Use | TanStack hook |
|---|---|---|
| Single fetch, cached in UI | rpc.queryOptions(tag, { input }) |
useQuery |
| Cursor-paginated list (one page per fetch) | rpc.cursorInfiniteOptions(tag, { baseInput, ... }) |
useInfiniteQuery |
| Generic page-param pagination | rpc.infiniteOptions(tag, { input, initialPageParam, getNextPageParam }) |
useInfiniteQuery |
| Fetch all cursor pages into one result | createFetchCursorItemsAsMergedPageQueryFn or fetchCursorPagesAsMergedPage |
custom queryFn / imperative |
| Server-push stream (chunk array in cache) | rpc.streamedOptions(tag, { input, queryFnOptions }) |
useQuery |
| Server-push stream (latest chunk only) | rpc.liveOptions(tag, { input }) |
useQuery |
| Write | rpc.mutationOptions(tag) |
useMutation |
| Invalidate a procedure group | queryClient.invalidateQueries({ queryKey: rpc.key("properties") }) |
- |
| Imperative one-off, no cache | rpc.call(tag, input, options) |
- |
bun add effect-rpc-tanstack-query effect@4.0.0-beta.78 @tanstack/query-core
# React apps
bun add @tanstack/react-queryPeer dependencies: effect@4.0.0-beta.78 and @tanstack/query-core.
Runtime support: Node >=18. This release targets Effect v4's unstable RPC APIs and is intentionally beta-version-specific, so keep the installed Effect version exactly aligned with the peer dependency.
import {
createRpcQueryUtils,
type FlatRpcAsyncIterableClient,
type FlatRpcPromiseClient,
} from "effect-rpc-tanstack-query";
import type { AppRpcs } from "./rpc-contract";
declare const rpcClient: FlatRpcPromiseClient<AppRpcs>;
declare const rpcStreamClient: FlatRpcAsyncIterableClient<AppRpcs>;
export const rpc = createRpcQueryUtils<AppRpcs>(rpcClient, { callStream: rpcStreamClient });This package does not create transports. Pass flat Effect RPC clients that call procedures by tag string, for example clients built with RpcClient.make(AppRpcGroup, { flatten: true }). Use makeEffectRpcClients to adapt that native scoped flat client without erasing its procedure types. The lower-level makeEffectRpcPromiseClient and makeEffectRpcAsyncIterableClient helpers adapt already-separated, fully provided Effect/Stream clients. All three wire abort signals to Effect fiber and stream-pull interruption. The complete HTTP RPC integration lives in examples/effect-http-rpc-vite/apps/web/src/rpc.ts.
import { skipToken } from "effect-rpc-tanstack-query";
import { useQuery } from "@tanstack/react-query";
const propertyId = "…";
const detail = useQuery(
rpc.queryOptions("properties.getDetail", {
input: propertyId ? { propertyId } : skipToken,
}),
);queryOptions passes TanStack's signal to the RPC client. The supplied
makeEffectRpcPromiseClient adapter passes it to Effect.runPromise, while
makeEffectRpcAsyncIterableClient interrupts active stream pulls.
skipToken behavior (deliberate deviations from oRPC):
- A
skipTokenquery is force-disabled after the user option spread, so a caller'senabledcan never re-enable a query that must not run (the types narrowenabledtofalsewhen the input is exactlyskipToken). skipTokenis omitted from the generated key. Symbols are dropped byJSON.stringify, so a key containing it would share its TanStack hash with the input-less key for the same procedure (cache cross-contamination) and would not survive persistence.
queryOptions, infiniteOptions, cursorInfiniteOptions, streamedOptions, liveOptions, and mutationOptions accept callOptions (everything in RpcCallOptions except signal, which TanStack owns) and forward it to the client on every call:
rpc.queryOptions("properties.getDetail", {
input: { propertyId },
callOptions: { headers: { "x-request-source": "desktop" } },
});import { useMutation, useQueryClient } from "@tanstack/react-query";
const queryClient = useQueryClient();
const updateProperty = useMutation({
...rpc.mutationOptions("properties.update"),
onSuccess: async () => {
await queryClient.invalidateQueries({ queryKey: rpc.key("properties") });
},
});Keys use [pathSegments, { input?, type?, fnOptions? }] with undefined fields omitted for partial invalidation:
rpc.key(); // match all RPC queries
rpc.key("properties"); // match all properties.* queries
rpc.key("properties.getDetail"); // match that procedure
rpc.queryKey("properties.list", { input: { search: "SW1" } }); // exact query keykey() is typed against every dot-separated prefix of the contract's tags, so rpc.key("properties") typechecks while rpc.key("nonexistent") is a compile error.
Generated keys can be replaced per call site when you need to slot into an existing cache layout. Pass queryKey to queryKey/queryOptions/infiniteOptions/cursorInfiniteOptions/streamedOptions/liveOptions (or mutationKey to mutationKey/mutationOptions) and it is used verbatim instead of the generated key:
rpc.queryOptions("properties.list", {
input: { search: "SW1" },
queryKey: ["legacy", "properties", "SW1"],
});Stored example:
[["properties", "getDetail"], { input: { propertyId }, type: "query" }];Generic page param:
useInfiniteQuery(
rpc.infiniteOptions("properties.list", {
input: (cursor: string | undefined) => ({ cursor, limit: 20 }),
initialPageParam: undefined,
getNextPageParam: (page) => page.pageInfo.nextCursor,
}),
);Cursor lists (pageInfo.hasNextPage / nextCursor):
useInfiniteQuery(
rpc.cursorInfiniteOptions("properties.list", {
baseInput: { search: "SW1" },
pageLimit: 200,
initialCursor: undefined,
}),
);Fetch all pages into one result (custom queryFn):
import { createFetchCursorItemsAsMergedPageQueryFn } from "effect-rpc-tanstack-query";
const searchInput = { search: "SW1" };
const mergedListKey = rpc.key("properties.list", {
input: { ...searchInput, cache: "merged" },
type: "query",
});
useQuery({
...rpc.queryOptions("properties.list", {
input: searchInput,
queryKey: mergedListKey,
}),
queryFn: createFetchCursorItemsAsMergedPageQueryFn(rpc, "properties.list", {
baseInput: searchInput,
pageLimit: 200,
totalLimit: 1000,
maxPages: 20,
}),
});Merged-page fetchers stop after 1,000 pages by default, accept a lower
maxPages, and reject repeated cursors before refetching the same page. Use a
distinct queryKey for merged-list queries if you also cache normal one-page
properties.list results for the same input.
For Effect RPC procedures declared with Rpc.make(..., { stream: true }). Effect represents these internally with RpcSchema.Stream. Pass callStream: rpcStreamClient when creating utils.
// Accumulating chunks in cache (Chunk[])
useQuery(
rpc.streamedOptions("events.stream", {
input: { count: 10 },
queryFnOptions: { refetchMode: "reset", maxChunks: 100 },
}),
);
// Latest chunk only
useQuery(rpc.liveOptions("events.stream", { input: { count: 10 } }));Per-procedure declared errors, stream errors, server/client middleware errors,
and RpcClientError transport/protocol failures flow into DataTag metadata
and generated option error types.
Export a configured helper from your app's RPC module:
export const rpc = createRpcQueryUtils<EffectRpc>(rpcClient, { callStream: rpcStreamClient });Use rpc.* for TanStack Query call sites instead of ad-hoc cache keys.