Skip to Content
Effetch Query
The client

Client members

Every member of a query client.

A client has the following members, and QueryClient has two functions that work with one:

MemberWhat it is
client.runPromise, client.runPromiseExitRun an Effect with the client, your services and Reactivity.
client.fetch, prefetch, invalidate, refetch, reset, clearThe cache functions for code outside Effect (Use the cache outside Effect).
client.registryThe AtomRegistry holding the cache, for @effect/atom-react and your own atoms.
client.dispose()Drops the cache, interrupts what's in progress and closes the layer; resolves once it's done.
QueryClient.layer(options)A client as a Layer<QueryClient>, disposed with its scope: for Effect programs and tests.
QueryClient.disposeAfter(client, response)Returns response with its body wrapped so that client is disposed once the body is sent.

runPromise is for code outside Effect, such as a router loader. If the Effect fails, it rejects with its error (Cause.squash), as Effect.runPromise does; runPromiseExit resolves with the Exit instead. Pass { signal } to interrupt the Effect when the signal aborts. The Reactivity it provides is the one the cache listens to, so Reactivity.invalidate run here refetches queries that declare those keys.

registry is yours to read and to add atoms to, but don't reset or dispose it: use client.dispose(). Its onNodeAdded and onNodeRemoved hooks are the client's, and replacing them breaks invalidation.

dispose() releases everything the client owns: the cache and its timers, then fetches, mutation runs and the Effects runPromise started, then the layer's scope. An interrupted mutation run doesn't roll back its optimistic writes, since the cache is already gone. Don't use the client afterwards.

disposeAfter is for a client made per request on a server that answers with a streamed Response, where the handler returns before the streamed results are sent. The client is disposed when the body ends, fails or the reader cancels it. Send the response it returns: one that's discarded is never read, so the client is never disposed. Stream the cache and React Router use it.

Inside Effect, the client is the QueryClient.QueryClient service. Queries, mutations and the client's hooks already run with it; elsewhere, run the Effect with client.runPromise or provide QueryClient.layer. In a React component, useQueryClient() returns the client of the nearest QueryProvider, for running Effects from event handlers.