Skip to Content
Effetch Query
Queries

Queries from endpoints

Turn Effetch endpoints into cached queries with fromClient.

Your API is already described as Effetch endpoints. fromClient turns each one into a cached query, so every component that shows GET /todos shares one fetch. This page shows what each query takes from its endpoint: its key, its errors and its schema.

The examples import todos.ts from Getting started.

Call a query for an endpoint

api.query takes an endpoint and returns a family: a function that gives you the query for one call. Call it with the endpoint's params and query, as you would call the endpoint:

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

export const  = ({ : { : 1 } });
// the query keyed ["todos", 1]

export const  = ({ : { : false } });
// the query keyed ["todos", { done: false }]

Reading the query, with useQuery in a component or Query.fetch in an Effect, sends the request through your Effetch client. The client keeps the response, and every reader of the same call shares it.

The argument a call takes is an EndpointQueryArgs: the route's params, and a query object, typed by the endpoint's query schema if it has one. Use the type for a helper that builds one, as EndpointQueryArgs<"GET /todos/:id">.

api.infiniteQuery and api.mutation work the same way, for infinite queries and mutations. All three take a query's or mutation's options, such as staleTime, and add what the endpoint already knows.

What a query takes from its endpoint

The endpoint describes the request, so the query needs little else:

  • Its key comes from the route. The key is the path with its params filled in, then the query object: GET /todos/:id called with { params: { id: 1 } } is ["todos", 1]. Invalidating ["todos"] reaches every todos endpoint. Query keys has the details.
  • Its errors are typed from the endpoint and the client. todo's error is NetworkError | DecodeError | HttpError.With<{ message: string }>, plus your plugins' errors if you use any. To turn a status into an error of your own, such as a 404 into TodoNotFound, see Query errors.
  • Its schema is the endpoint's output. The value is decoded with it, and it's what the value crosses to the browser with when you render on the server, so Server rendering needs nothing more.
  • Only endpoints without a body are queries: GET, HEAD, OPTIONS and TRACE. Everything else is a mutation, whose variables are the call's options.

A family is also a target. Query.invalidate(todo) reaches every todo({ params }) call, and nothing else: not GET /todos, and not GET /todos/:id/comments, though their keys start the same way.

Which calls share a query

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: then they're compared as they're sent.

Define one api.query per endpoint, as todos.ts does. Two families for the same route make the same keys but keep separate caches: invalidating one leaves the other stale, and sending the cache from the server throws a DuplicateQueryKeyError.

Headers and plugins

A call's arguments make its key. A header shouldn't change which cached value you get, so headers and plugins go in the query's options, as they do in Effetch's bind:

import * as  from "effect/Schema";

import {  } from "@monarcode/effetch";

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

export const  = .(
  .("GET /greeting", { : . }),
  { : { "accept-language": "fr" } },
);
// greeting() is the query keyed ["greeting"], whatever its headers

fromClient options

Most apps need none of these: fromClient(ApiClient) is enough. Each one is for a single situation:

OptionUse it whenDefault
keyYou call two backends with the same paths, and their keys would clash. With key: ["billing"], GET /invoices is ["billing", "invoices"].No prefix
invalidates: "route"Every mutation should invalidate the queries under its route's key when it succeeds: PATCH /todos/:id invalidates ["todos"]. See Invalidate after a mutation.A mutation invalidates only what it names
defaultError, statusErrorsYour client isn't a Fetch.Service class, so fromClient can't read them from it. See What's sent.Read from the Fetch.Service class

What's next

On this page