Queries

Use #[rpc_query] for read-only operations. Wrap them with createQuery in Svelte for reactive, auto-refetching data.

rust
#[rpc_query]
async fn get_user(id: u32) -> User {
    db::find_user(id).await
}
ts
// Direct call
const user = await rpc.query('get_user', 42);

// With options
const user = await rpc.query('get_user', 42, {
  timeout: 5000,
});
ts
// Reactive wrapper — auto-refetches when input changes
let userId = $state(42);
const user = createQuery(rpc, 'get_user', () => userId);

// user.data       — User | undefined
// user.isLoading  — boolean
// user.isError    — boolean
// user.refetch()  — manual refetch
Note: Input structs must derive both Deserialize (for JSON parsing) and Serialize (so the CLI scanner can emit TypeScript interfaces). Without Serialize, the struct won't appear in the generated types.

Per-Call Options

Every query() call accepts an optional trailing CallOptions object to override client-level defaults.

ts
interface CallOptions {
  headers?: Record<string, string>;  // merged with client headers
  timeout?: number;                   // override client timeout (ms)
  signal?: AbortSignal;              // combined with client signal
  dedupe?: boolean;                  // per-call dedup override
}
ts
// Void-input query with options
await rpc.query('time', { timeout: 5000 });

// Query with input + options
await rpc.query('get_user', { id: 1 }, {
  timeout: 5000,
  headers: { 'X-Request-Id': crypto.randomUUID() },
});

Try it

Math — Struct Input, Result<T, E>

Demonstrates struct input and Result<T, E> error handling.

visit GitHub to learn more about metaxy