Skip to Content
Effetch Query
The cache

Read and write the cache

Read, write and invalidate cached values from plain code and from Effect.

Components aren't the only code that needs the cache. A router loader fetches before a screen shows, a pull-to-refresh handler refetches, and a socket message writes what changed. This page shows how to reach the cache from plain code, such as loaders and event handlers, and from Effect, and what each cache function does.

The examples import api.ts, client.ts and todos.ts from Getting started.

Read and write outside Effect

Code outside Effect can't yield* a cache function, so the client has a method for six of them. Each returns a Promise and does what its Query function does:

MethodReturns
client.fetch(query)Promise<A>, rejected as the query fails, so a router's redirect gets through.
client.prefetch(query)Promise<void>, resolved when the fetch settles. Never rejects.
client.invalidate(target)Promise<void>, resolved at once.
client.refetch(target)Promise<void>, resolved when the queries in use have settled.
client.reset(target)Promise<void>, resolved at once.
client.clear(target)Promise<void>, resolved at once.

Here they are in a loader and two event handlers:

import {  } from "./client.js";
import { ,  } from "./todos.js";

// A router loader. It rejects as the query fails, and stops waiting when
// the navigation is cancelled.
export const  = (: number, : AbortSignal) =>
  .(({ : {  } }), {  });

// Hovering a link starts the next screen's fetch. It never rejects.
export const  = () => {
  void .(({ : { : false } }));
};

// Pull to refresh: the spinner stops after the lists in use have settled.
export const  = () => .();

A few rules hold for every method:

  • Without a target, invalidate, reset and clear act on every query, and refetch on every query in use.
  • invalidate, refetch, reset and clear reject only with a defect, such as a Query.KeyHoldsTargetError for a key that holds queries.
  • On a disposed client, invalidate, refetch, reset and clear do nothing.
  • fetch and prefetch take { signal } to stop waiting when it aborts.

The client has no get, set or update method. To read the cache, use QueryClient.get, as the next section shows. To write it, run Query.set or Query.update with queryClient.runPromise, as Write to the cache shows.

Read without waiting

To read the cache where you can't wait for a promise, such as a form listener, QueryClient.get(queryClient, query) returns the cached value as an Option, as Query.get does. It never fetches:

import * as  from "effect/Option";

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

import {  } from "./client.js";
import {  } from "./todos.js";

export const  = (: number): string =>
  .(.(, ({ : {  } })), {
    : () => "",
    : () => .,
  });

Read and write from Effect

Each cache function is an Effect that needs the client. Inside queries, mutations and the client's hooks, the client is already there, so yield* them. Elsewhere, run the Effect with queryClient.runPromise. This loader opens a todo and warms the cache for the next screen:

import * as  from "effect/Effect";

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

import {  } from "./client.js";
import { ,  } from "./todos.js";

const  = (: number) =>
  .(function* () {
    // The cached value if it's fresh, otherwise a fetch.
    const  = yield* .(({ : {  } }));
    // Warm the cache for the next screen. It waits for that fetch, but
    // never fails.
    yield* .(({ : { : . } }));
    return ;
  });

export const  = (: number) => .(());

Query.fetch fails as the query does, with its typed error, so loadTodo rejects when the todo can't load. Query.prefetch never fails: a failure shows in the query's result instead.

queryClient.runPromise(effect) runs any Effect with your services and the client, and rejects with its error if it fails. queryClient.runPromiseExit(effect) resolves with the Exit, Effect's value for a success or a failure with its Cause, so it never rejects. Both take { signal } to interrupt the Effect when the signal aborts. In a component, useQueryClient() returns the client.

The cache functions

The functions that act on many queries at once take a target: a query, a family, a key or a group of them, as Targets explains. A key that holds a query, a family or a group matches nothing, and the function dies with a Query.KeyHoldsTargetError.

The following table lists the cache functions. Each returns an Effect, except Query.group, which makes a target:

FunctionWhat it does
Query.fetch(query)The value if fresh, otherwise a fetch. Joins a fetch in progress. Fails as the query does.
Query.prefetch(query)Fetches if not fresh, and ignores the result. Never fails.
Query.get(query)The cached value as an Option. Never fetches.
Query.set(query, value)Writes a value. It counts as fetched at the time of the write, so it's fresh for staleTime.
Query.update(query, f)Writes f(value); does nothing without a value.
Query.update(family, f)Writes f(value, args) to every cached query of a family, such as todos.
Query.invalidate(target)Marks queries stale: those in use refetch at once, the rest when next used.
Query.refetch(target)Refetches the queries in use, and completes when they've settled.
Query.reset(target)Drops the values; queries in use fetch again.
Query.clear(target)Drops the values without fetching; queries in use show Initial until something asks.
Query.group(...targets)One target for several: queries, families, keys or groups. Each matching query once.

No function cancels a fetch in progress and keeps the query's value, as TanStack Query's cancelQueries does. What a rollback doesn't cover describes the race that leaves.

Write to the cache

A write is for a change that reached you without a fetch, such as a socket message saying a todo was saved. This handler writes the todo into its own query and into every cached list:

import * as  from "effect/Effect";

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

import type {  } from "./api.js";
import {  } from "./client.js";
import { ,  } from "./todos.js";

export const  = (: ) =>
  .(
    .(
      .(({ : { : . } }), ),
      .(, () =>
        .(() => (. === . ?  : )),
      ),
    ),
  );

A few details matter when you write to the cache:

  • A written value shows until the next fetch settles. It stays while that fetch is in progress, and if the fetch fails, it stays as the query's last value. A query that isn't cached yet is cached with it, without fetching. The client's queries hooks don't run for a write.
  • Query.update on a family writes only what's cached. It writes the family's own queries that have a value, and creates none. Writes don't take a group, since one value can't fit queries of different types.
  • Query.get keeps the last value. A query whose last fetch failed still returns the value it had before.
  • In use means subscribed. A query is in use while something subscribes to it, or to an atom or query that depends on it: a component that shows it, or a query in use that reads it through ctx.result, as Queries that read other queries shows. A dependent that's only cached doesn't count.

In a mutation's optimistic, Query.set and Query.update writes are undone if the run fails, as Optimistic updates shows. A record held in many queries is easier to write through an entity.

When you're done with a client, such as one made per request on a server, queryClient.dispose() releases everything it owns, as The query client describes.

What's next

On this page