Skip to Content
Effetch Query
Mutations

Invalidate after a mutation

Mark the queries a mutation changes as out of date.

After a todo is saved, the lists that show it are out of date. A mutation invalidates nothing unless you say so: invalidating a query, TanStack Query's invalidateQueries, marks it stale, so the queries on screen refetch and the rest refetch when they're next used. This page shows the three ways to say what a mutation invalidates, from the most precise to the most automatic, and how to wait for the refetch.

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

Invalidate per mutation

invalidates lists the targets the mutation changes: queries, families, keys or groups. Getting started's toggleTodo invalidates [todos]:

import * as  from "effect/Schema";

import {  } from "@monarcode/effetch";

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

export const  = .(
  .("PATCH /todos/:id", {
    : .({ : . }),
    : ,
  }),
  { : [] },
);

todos is the family for GET /todos, and a family matches exactly its own queries. So [todos] marks every cached todo list stale, whatever its filter, and leaves todo, the GET /todos/:id query, as it was, though its key ["todos", 1] starts the same way. To reach the detail too, list both, [todos, todo], or give the key ["todos"], which reaches every query whose key starts with it.

To pick targets from what the server sent back, give a function of the run's value and variables. This one invalidates the lists and the one todo it saved:

import * as  from "effect/Schema";

import {  } from "@monarcode/effetch";

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

export const  = .(
  .("PATCH /todos/:id", {
    : .({ : . }),
    : ,
  }),
  {
    : () => [, ({ : { : . } })],
  },
);

A failed run invalidates nothing.

Each entry of the list is one target. An entry that's an array holding a query, a family or a group is a key, and matches nothing: invalidates: [[todos, todo]] is a mistake. In a list, the run dies with a Query.KeyHoldsTargetError before optimistic or the request. From the function, the change is already saved, so the target is logged and skipped. Write invalidates: [todos, todo] instead, or Query.group(todos, todo) where one target is needed. Targets lists what each kind of target matches.

Invalidate by route

Give fromClient invalidates: "route", and an endpoint mutation that succeeds invalidates its route's queries: those under the route's literals before its first param. PATCH /todos/:id invalidates ["todos"], so every todo list and every todo is marked stale:

import * as  from "effect/Schema";

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

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

const  = (, { : "route" });

// Invalidates ["todos"] when it succeeds.
export const  = .(
  .("PATCH /todos/:id", {
    : .({ : . }),
    : ,
  }),
);

// A mutation's own invalidates replace the route's, and [] turns them off.
export const  = .(.("POST /todos/:id/seen"), {
  : [],
});

It invalidates more than a write changes, on purpose: DELETE /todos/:id/comments/:cid invalidates every query under ["todos"]. A needless refetch costs a request; a missed one shows stale data. Two kinds of route need care:

  • A route with no param invalidates its whole path: POST /todos/reorder invalidates ["todos", "reorder"], which no list is keyed under. Give it invalidates: [todos].
  • A route that starts with a param, such as /:tenant/todos, invalidates every query under the key given to fromClient, or every query on the client if fromClient was given no key.

Invalidate everything

To invalidate every query after any mutation succeeds, say it once, on the client:

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

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

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

[] is the empty key, which every key starts with.

Wait for the refetch

invalidates doesn't wait: it marks the queries stale and the run settles, while the ones in use refetch in the background. To settle the run only after they have the new data, call Query.refetch in onSuccess instead. It refetches the matching queries in use, and completes when they've all settled:

import * as  from "effect/Schema";

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

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

// The run settles after every todos list in use has the new todo.
export const  = .(
  .("POST /todos", {
    : .({ : . }),
    : ,
  }),
  { : () => .() },
);

That matters when the screen shows something until the server's data arrives, as Optimistic updates does. A refetch reaches only queries in use. The others aren't marked stale, so the next time they're used they refetch only if their staleTime has passed. Freshness and refetching defines "in use".

What's next

On this page