Skip to Content
Effetch Query
Queries

fromClient options

The options fromClient takes, and how to use a client that isn't a Fetch.Service class.

fromClient takes your client's service and, optionally, a few options that apply to every query and mutation it makes. Most apps need none of them; fromClient(ApiClient) is enough. Each option is for one situation:

OptionUse it whenDefault
keyYou call two backends with the same paths, and their keys would clash (Query keys).No prefix
invalidates: "route"Every mutation should invalidate its own route's queries when it succeeds.Only what each mutation names
defaultError, statusErrorsYour client isn't a Fetch.Service class, and you render on the server (below).Read from the Fetch.Service class

invalidates: "route" is explained in Invalidate after a mutation.

A client that isn't a Fetch.Service class

This part matters only if you render on the server, and you made your Effetch client by hand instead of declaring it with Fetch.Service.

Why it's needed. When a query fails on the server, Effetch Query can send the error to the browser, so the browser shows the same error instead of fetching again (Server rendering). To send an error, it needs the schemas the client decodes errors with: its defaultError and its statusErrors. A Fetch.Service class carries them, so fromClient reads them from it. A client made with Fetch.make and provided through a Context.Service key doesn't: its config is only there once its layer is built, which is after your queries are defined.

What to do. Keep the client's config in one object. Make the client from it, and give fromClient the same object:

import * as  from "effect/Context";
import type * as  from "effect/Effect";
import * as  from "effect/Schema";

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

class  extends .<>()(
  "SessionExpired",
  { : . },
) {}

const  = {
  : "https://billing.example.com",
  : .({ : . }),
  : { 401:  },
};

export const  = .();

export class  extends .<
  ,
  .<typeof >
>()("BillingClient") {}

declare module "@monarcode/effetch-query" {
  interface Register {
    readonly : ;
  }
}

// Reads defaultError and statusErrors, and ignores the rest.
export const  = (, {
  : ["billing"],
  ...,
});

export const  = .(
  .("GET /invoices", { : .(.) }),
);
// invoices() is the query keyed ["billing", "invoices"]
export const  = .(["billing"]);

The example also uses key: ["billing"], so its queries can't clash with another backend's, and Query.refetch(["billing"]) reaches all of them.

If you leave them out, nothing changes in the browser: the client still decodes your errors, and they're still typed. A query that failed on the server just isn't sent, so the browser fetches it again.

On a Fetch.Service class, the same two options override what the class was declared with. You need that only when the class's layer builds its client from a different config.

On this page