Skip to Content
Effetch Query
The client

Defaults and app-wide hooks

Set defaults and hooks for every query and mutation of a client.

You want every query fresh for 30 seconds, or every failure logged, without repeating it in each definition. The client takes both: defaults that each query or mutation can override, and hooks that run for all of them.

Set defaults

queries.defaults sets options for every query, and mutations.defaults for every mutation. A query's or mutation's own options override them. Each default, and its value when you don't set it, is as follows:

DefaultWhat it doesWhen unset
queries.defaults.staleTimeHow long a success counts as fresh.0, stale at once
queries.defaults.retryRetries a failed fetch on this Schedule. Defects aren't retried.No retries
queries.defaults.refetchOnMountRefetches a stale query when a component starts using it.true
queries.defaults.retryOnMountRefetches a failed query when a component starts using it.true
queries.defaults.refetchOnFocusRefetches a stale query in use when the app regains focus.true
queries.defaults.refetchOnReconnectRefetches a stale query in use when the device reconnects.true
queries.defaults.refetchIntervalRefetches a query in use at this interval.No polling
mutations.defaults.retryRetries a failed run on this Schedule. Defects aren't retried.No retries
mutations.defaults.concurrentHow one use's runs overlap: true, false, "queue" or "drop".true

A query is in use while something subscribes to it, such as a mounted component that reads it. gcTime, reactivityKeys and entities aren't defaults: each query sets its own, because they're fixed when the query is defined.

Query options explains each query option, and When runs overlap explains concurrent. In types, the two groups are QueryClient.QueryDefaults and QueryClient.MutationDefaults.

Run a hook for every query or mutation

App-wide hooks run for every query or mutation, with your services, for logging or error reporting. This client sets query defaults and logs every failure:

The example imports api.ts from Getting started.

import * as  from "effect/Cause";
import * as  from "effect/Effect";

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

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

export const  = .({
  : ,
  : {
    : { : "30 seconds", : false },
    : ({ ,  }) =>
      .("Query failed", , .()),
  },
  : {
    // Runs after the mutation's own onFailure.
    : ({  }) =>
      .("Mutation failed", .()),
  },
});

Each hook runs once per fetch or run. The success, failure and settled hooks run after retry has succeeded or given up. mutations.onMutate runs before the run starts, so before its retries too. An interrupted fetch or run, such as a fetch a refetch replaces or a run whose client is disposed, runs no success, failure or settled hook.

A hook is a QueryClient.Hook<Event>: a function from the event to an Effect that can't fail, so handle errors inside it. A hook that dies is logged, and the others still run. Query hooks run in a fiber of their own, after the query shows its result, so a slow hook doesn't hold the query up. Mutation hooks are part of the run: it completes after them.

When each hook runs

Each hook receives an event, and runs at a set point, as follows:

HookWhen it runsReceives
queries.onSuccessOnce per successful fetch; not for Query.set.QuerySucceeded: key, value
queries.onFailureOnce per failed fetch, after retry.QueryFailed: key, cause
queries.onSettledOnce per fetch that succeeds or fails, after onSuccess or onFailure.QuerySettled: key, exit
mutations.onMutateBefore each run's run and its retries, after its optimistic writes.MutationStarted: mutation, variables
mutations.onSuccessAfter each successful run, after its own onSuccess and invalidates.MutationSucceeded: mutation, variables, value
mutations.onFailureAfter each failed run: its optimistic writes are rolled back, then its own onFailure runs.MutationFailed: mutation, variables, cause
mutations.onSettledAfter each run that succeeds or fails, after its own onSettled.MutationSettled: mutation, variables, exit

A cause is Effect's Cause, the full record of why the fetch or run failed, and an exit is its Exit: the value or the Cause. A mutation event's mutation is the mutation's definition, so you can compare it with yours. The event types live in the QueryClient namespace, as do QueriesOptions and MutationsOptions, the types of the two groups.

What's next

On this page