Skip to Content
Effetch Query
The client

The query client

Create the query client, and give it the services your queries use.

The query client holds the cache and runs your queries and mutations with your services. This page covers what a client takes: its options, the layer or runtime that provides your services, and how those services are typed. Defaults and app-wide hooks, Focus and online signals and Client members cover the rest.

The examples import api.ts from Getting started.

A client is cheap to create. A browser app makes one with QueryClient.make. A server makes one per request, from the process's runtime, and disposes it when the response is done. In an Effect program or a test, QueryClient.layer makes one as a layer, disposed when the layer's scope closes.

Options

QueryClient.make and QueryClient.layer take the following options:

OptionWhat it doesDefault
layerProvides your registered services. Built the first time it's needed and kept until dispose().Required, unless you give runtime or Register declares no services
memoMapThe Layer.MemoMap to build layer with, so layers built with it before are shared rather than built twice.The client's own; for QueryClient.layer, that of the layer graph it's built in
runtimeInstead of layer: a ManagedRuntime that provides your services. The client uses it and doesn't dispose it.None
signalsWhere the client hears that the app regained focus or the device came back online.Signals.browser
queriesdefaults for every query, and the onSuccess, onFailure and onSettled hooks.No defaults, no hooks
mutationsdefaults (retry, concurrent) for every mutation, and the onMutate, onSuccess, onFailure and onSettled hooks.No defaults, no hooks
entitiessync: the entities whose cached copies a fetch keeps in sync (Keep copies in sync).None

If layer fails to build, everything that needs it dies with that failure.

In types, QueryClient.Options is one of two shapes. LayerOptions gives the client a layer (and optionally a memoMap), which it builds itself. RuntimeOptions gives it a runtime instead. Both extend CommonOptions: signals, queries, mutations and entities.

QueryClient.OptionalOptions is the parameter list of make and layer. An app that declares no services in Register can leave out layer and runtime, or every option: QueryClient.make(). Once Register declares services, a client must be given them.

Layer or runtime

Given a layer, the client builds your services itself, once, the first time it needs them. That's the setup in Getting started.

Sharing your runtime. If your app already has a ManagedRuntime that provides your services, give the client that instead of a layer. It uses the runtime's services rather than building its own, and leaves the runtime running when it's disposed:

import * as  from "effect/ManagedRuntime";

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

import {  } from "./api.js";

export const  = .();

export const  = .({ :  });

This is how a server works: one runtime per process, and a client per request that reuses its services rather than building them again. If the runtime fails to build, or has been disposed, everything that needs it dies.

Clients of one runtime share its Reactivity, so an invalidation by any of them, or by a service the runtime built (an SQL client, say), refetches the matching queries of all of them. No cached values cross between clients.

Services on top of a runtime's. When a client needs services of its own on top of the runtime's, give it a layer with the runtime's memoMap instead. What the runtime has built is shared, not built again, and so is its Reactivity.

Typed services

Register declares the services your queries and mutations run with, once for the whole app (1. Describe your API). Two types read it back, for typing Effects of your own:

  • QueryClient.Services is the services declared in Register, or never without the declaration.
  • QueryClient.Requirements is what queries, mutations and the client's hooks run with: Services | QueryClient.

An Effect that needs only QueryClient.Requirements can be a query's run or what a mutation's run returns, and client.runPromise runs it.

On this page