Skip to Content
Effetch Query
Queries

Query keys

How a key finds a cached value, how an endpoint's key is built, and how a target picks the queries to act on.

Every query has a key, an array such as ["todos", 1]. The cache finds a query's value by its key, and you name the queries to refetch or clear by a key or part of one. With endpoints, the key comes from the route; with Query.make and Query.family, you write it. This page shows how keys are built, and how a target picks the queries to act on.

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

How keys are built

Keys are hierarchical, so put the broadest part first: invalidating ["todos"] reaches every key that starts with it. Parts are compared by value (Equal.equals), so { done: false } matches { done: false }.

A family's key is a prefix. Each member's key is the family's key with its argument added: Query.family({ key: ["todos", "detail"] }) called with 1 is ["todos", "detail", 1].

Keep keys to JSON values: strings, numbers, booleans, null, arrays and plain objects. When you render on the server, keys are sent to the browser, and sending the cache throws on a key that isn't JSON, as Keys on the server explains.

How an endpoint's key is built

An endpoint query's key is its route's path with the params filled in, then the query object. The method, scheme and host aren't part of it:

CallKey
todo({ params: { id: 1 } })["todos", 1]
todos()["todos"]
todos({ query: { done: false } })["todos", { done: false }]

The family's own key is the path up to the first param, so todo.key is ["todos"].

Two calls that send the same request share one query, and one fetch. Only what's sent counts, so these make no difference to the key:

  • params the path doesn't name;
  • arrays left empty;
  • null and undefined query values and array items.

Values are otherwise compared as given. todo({ params: { id: 1 } }) and todo({ params: { id: "1" } }) send the same request but are two queries, so convert a route's params before you call a query, with Number(params.id). Query values work the same way, so { page: 1 } and { page: "1" } are two queries, unless the endpoint has a query schema, as Keys from a query schema shows.

A prefix for each backend. To call a second backend with the same paths, give fromClient a key: fromClient(Client, { key: ["billing"] }) puts "billing" before every key it makes, so GET /invoices is ["billing", "invoices"]. See fromClient options.

Keys from a query schema

An endpoint with a query schema (Effetch's query option on Fetch.endpoint) is keyed by its query as it's sent: each value encoded to a string, as in the URL. A Date is its ISO string, 2 is "2", and a field renamed with Schema.encodeKeys has its sent name. So equal requests share a query, and the key is JSON for server rendering:

import * as  from "effect/Schema";

import {  } from "@monarcode/effetch";

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

export const  = .(
  .("GET /todos/search", {
    : .(),
    : .({
      : .,
      : .(.),
    }),
  }),
);

({ : { : new ("2026-11-16"), : 2 } });
// keyed ["todos", "search", { due: "2026-11-16T00:00:00.000Z", page: "2" }]

Calling searchTodos throws an EndpointQueryError when:

  • the query doesn't match the schema;
  • a field doesn't encode to a string or a list of strings;
  • a field is Redacted: the key would hold the secret and send it to the browser, so send secrets in a header.

The schema's encoding can't need services, since the key is made when you call searchTodos, outside any Effect. KeyQuerySchema is the type of such a schema: check one with satisfies KeyQuerySchema. For an infinite query, pageParam names one of the schema's optional fields, as Infinite queries shows.

Targets

Many functions act on a set of queries: Query.invalidate, Query.refetch, Query.reset, Query.clear, a mutation's invalidates, useRefetch and useIsFetching, among others. Each takes a target, which says which queries to act on. A target is one of these:

  • an endpoint family, such as todos: the queries it made, and no other route's, even when their keys start the same way;
  • a family from Query.family: every query whose key starts with the family's;
  • a query, such as todo({ params: { id: 1 } }): that key only;
  • a key, such as ["todos"]: every query whose key starts with it, so [] matches every query;
  • a group, made with Query.group: every query one of its targets matches.

A group combines targets that share no key, such as the queries one screen shows. It matches each query once, and is a target like any other:

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

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

// The two todos endpoints, and nothing else under ["todos"].
export const  = .(, );

export const  = .();

Query.group returns a Query.Group, whose targets lists what it combines; a group passed to it is flattened into it. Writes don't take a group, since one value can't fit queries of different types: Query.set takes one query, and Query.update one query or one family.

Pass the targets to Query.group one by one, not as an array. An array of queries is a key, not a group, and as a key it would match nothing, so Query.GroupTargets makes Query.group([todos, todo]) a type error. Where a target is expected, though, [todos, todo] compiles, since it's a valid key, and fails at run time with a Query.KeyHoldsTargetError:

  • the Query functions die with it, and so does a mutation's run for a key in invalidates;
  • the client's methods reject with it;
  • useRefetch, useIsFetching and QueryClient.dehydrate's queries throw it when called.

What's next

On this page