Skip to Content
Effetch Query
Reference

QueryClient

Every member of the QueryClient namespace: the client, its options and hooks, and sending the cache from a server.

QueryClient makes the client that holds the cache and runs your queries, and sends that cache from a server. Import it from the core entry point:

import {  } from "@monarcode/effetch-query";

Its exports follow, grouped by the guide that covers them, each with its docs. Your editor shows the same docs, with an example, when you hover the name.

The client

Guide: The query client.

QueryClient.make

Creates a QueryClient. Nothing is built until the client is first used: then it builds layer once and keeps it until QueryClient.dispose. Given a runtime instead, it uses the runtime's services and builds none of its own (RuntimeOptions). With no services declared in Register, the options can be left out (OptionalOptions).

QueryClient.QueryClient

A query cache with its own AtomRegistry.AtomRegistry, and your services from its own build of your layer or from your runtime. Create one with make (or layer in Effect programs). Every query, mutation and Effect-side cache function runs with it, as the QueryClient service.

A client is cheap to create. On a server, create one per request from your process's runtime (RuntimeOptions) and QueryClient.dispose it when the response is done.

The service every Effect-side cache function needs. Queries, mutations and the client's hooks already run with it; elsewhere, run the Effect with QueryClient.runPromise or provide layer.

Prop

Type

QueryClient.layer

Creates a QueryClient as a layer, for Effect programs and tests without React. The client is disposed when the layer's scope closes.

Unless options.memoMap is set, the client builds options.layer with the memo map of the layer graph it's built in, so services that graph also builds are built once. A client given options.runtime uses the runtime's services instead. With no services declared in Register, the options can be left out (OptionalOptions).

QueryClient.CommonOptions

The Options every client takes, whether it's given a layer or a runtime.

Prop

Type

QueryClient.LayerOptions

Options that give the client a layer, which it builds itself.

Prop

Type

QueryClient.OptionalOptions

The parameters of make and layer. With no services declared in Register, the options can be left out, or given without a layer or a runtime. With services declared, it's [options: Options], so a client must be given them.

QueryClient.Options

Options for make and layer: your services, as a layer (LayerOptions) or a runtime that already provides them (RuntimeOptions), with the CommonOptions.

QueryClient.Register

Declares the services your queries and mutations run with. Augment it once, next to your composition root, and every definition, client hook and QueryClient.runPromise is typed against it:

  • make requires a layer or a runtime that provides these services.
  • An Effect that needs a service outside them doesn't compile.

Without the augmentation, Services is never, so anything that needs a service fails to compile.

QueryClient.Requirements

What queries, mutations and the client's hooks run with: your Services and the QueryClient.

QueryClient.RuntimeOptions

Options that give the client a ManagedRuntime providing your services, built once for every client that uses it. Share one runtime per process: on a server, each request's client then reuses its services rather than building them again.

The client doesn't own the runtime: QueryClient.dispose leaves it running. The runtime builds its layer the first time any client needs it; if that fails, or the runtime has been disposed, everything that needs it dies.

The client builds its own parts with the runtime's memoMap, so it shares the runtime's Reactivity with the services the runtime built (an SQL client publishes on it) and with the runtime's other clients: a Reactivity.invalidate in any of them refetches the matching queries of all. No cached values cross between clients.

Prop

Type

QueryClient.Services

The services declared in Register, or never without the augmentation.

Defaults and app-wide hooks

Guide: Defaults and app-wide hooks.

QueryClient.Hook

An app-wide hook in QueriesOptions or MutationsOptions: it receives an event and runs with your services and the client. It can't fail; handle errors inside it. A hook that dies is logged, and the others still run. Query hooks run in a fiber of their own, after the query shows its result, so a slow hook doesn't hold the query up. Mutation hooks are part of the run: it completes after them.

QueryClient.MutationDefaults

Options every mutation takes, set for all of them in MutationsOptions.defaults. A mutation's own options (Mutation.MakeOptions) extend these, with E its error type.

Prop

Type

QueryClient.MutationFailed

What MutationsOptions.onFailure receives.

Prop

Type

QueryClient.MutationSettled

What MutationsOptions.onSettled receives.

Prop

Type

QueryClient.MutationsOptions

The mutations group of Options: defaults for every mutation, and hooks that run on every run, after the mutation's own callbacks. The hooks run with your services. Interruptions never reach them.

Prop

Type

QueryClient.MutationStarted

What MutationsOptions.onMutate receives.

Prop

Type

QueryClient.MutationSucceeded

What MutationsOptions.onSuccess receives.

Prop

Type

QueryClient.QueriesOptions

The queries group of Options: defaults for every query, and hooks that run on every fetch. The hooks run with your services, after the query's retry. Interruptions never reach them.

Prop

Type

QueryClient.QueryDefaults

Options every query takes, set for all of them in QueriesOptions.defaults. A query's own options (Query.Options) extend these, with E its error type.

Prop

Type

QueryClient.QueryFailed

What QueriesOptions.onFailure receives.

Prop

Type

QueryClient.QuerySettled

What QueriesOptions.onSettled receives.

Prop

Type

QueryClient.QuerySucceeded

What QueriesOptions.onSuccess receives.

Prop

Type

Keep copies in sync

Guide: Keep copies in sync.

QueryClient.EntitiesOptions

The entities group of Options: entities whose copies a fetch keeps in sync across the cache.

Prop

Type

Use the cache outside Effect

Guide: Read and write outside Effect.

QueryClient.get

Returns a query's cached value in client, if it has one, outside Effect: TanStack's queryClient.getQueryData. It reads as Query.get does, without fetching, for code that can't wait for a promise, such as a form listener. Inside Effect, use Query.get.

Send the cache

Guide: Send the cache.

QueryClient.dehydrate

Reads a client's cache to send to the browser, for server rendering, as TanStack's dehydrate. Load what the page needs first (Query.prefetch through QueryClient.runPromise), then send the result with the page and load it with hydrate (or HydrationBoundary from @monarcode/effetch-query/react).

Only queries with a schema are sent, encoded with it, timestamp included, so a query stays fresh in the browser as long as it would on the server. It reads without fetching: a stale query is sent as it is. Values a schema can't encode are left out.

Throws a NonJsonKeyError for a key that isn't JSON, a DuplicateQueryKeyError when two queries it sends have one key, and a Query.KeyHoldsTargetError when queries is a key that holds a query, a family or a group.

QueryClient.DuplicateQueryKeyError

Thrown by QueryClient.dehydrate when two cached queries it would send have one key: two definitions with the same key, such as two api.query of one Effetch endpoint. The browser finds a query by its key, so it couldn't tell them apart. Give one of them another key (an Effetch builder takes a key prefix).

QueryClient.NonJsonKeyError

Thrown when a query key has to become a string, to send a query from the server to the browser, and a segment isn't JSON: a Date, a class instance, undefined in an array, NaN or Infinity, a function, a bigint or a symbol. Keys of queries that cross the wire hold only strings, finite numbers, booleans, null, and arrays and plain objects of them (undefined properties are dropped, as JSON.stringify drops them).

QueryClient.DehydratedState

A client's cache as dehydrate sends it: Effect's Hydration format, so a framework's serializer carries it as it carries any atom state. Its values are JSON. With pending: "promise" (DehydrateOptions), a query still loading carries a promise of its result, which only streaming serializers (React's, TanStack Start's, React Router's) can send.

QueryClient.DehydrateOptions

Options for dehydrate.

Prop

Type

Load the cache in the browser

Guide: 3. Load the cache in the browser.

QueryClient.hydrate

Loads a cache dehydrate sent into a client, as TanStack's hydrate. A query that isn't cached shows its value the first time it's read, without fetching, and refetches once it's stale. A cached query takes it only if it's newer than the last data it has. A query the server was still loading waits for its result instead of fetching.

Values its schema can't decode are dropped, and the query fetches. State from Effect's own Hydration.dehydrate, for other atoms, is loaded too.

Client members

Guide: Dispose a client.

QueryClient.disposeAfter

Returns response with its body wrapped so that client is disposed once the body has been sent: when it ends, fails, or the reader cancels it (a client that disconnects). A response with no body disposes the client at once. Use it for a client made per request on a server that answers with a streamed Response, where the request's handler returns before its streamed results are sent: React Router's middleware, Hono, Bun.serve, Deno.serve.

Send the response it returns: one that's discarded is never read, so the client is never disposed. Only the status, status text and headers are kept, not a runtime's own options (Cloudflare's encodeBody, say), and the body goes out as a stream, so a fixed body loses the length the runtime would have sent. A body already read or locked throws, and the client is disposed all the same. A failed QueryClient.dispose is logged with console.error, not thrown.

The core uses ReadableStream and Response only when this is called, so it still loads where they don't exist, such as React Native.

On this page