Skip to Content
Effetch Query
Queries

Queries from any Effect

Define queries with Query.make and Query.family, from any Effect you write.

Not every query is an Effetch endpoint. You might call your Effetch client yourself, use Effect's own HttpClient, or read a database or local storage. Any Effect can be a query: Query.make defines one query, and Query.family one query per argument. This page shows both, and what you write yourself that an endpoint would give you.

Two things stay the same as with endpoints. Name the services your queries use in Register, as 1. Describe your API does, and give the client a layer that provides them. The Use Effect's HttpClient sample shows both.

Call your Effetch client yourself

Query.make takes a key and a run Effect. Query.family takes a key and a run function of one argument, and returns a family: a function that gives you the query for each argument. This file calls ApiClient from Getting started directly:

import * as  from "effect/Schema";

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

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

const  = .({ : ., : . });

export const  = .({
  : ["users", "me"],
  : .(() => .("/users/me", { :  })),
});
// me is the query keyed ["users", "me"]

export const  = .({
  : ["users", "detail"],
  : (: number) =>
    .(() =>
      .("/users/:id", { : {  }, :  }),
    ),
  : "1 minute",
});
// user(1) is the query keyed ["users", "detail", 1]

These work the same as endpoint queries, with the same typed errors from Effetch. Each takes the same options too, such as staleTime, which Query options lists.

Use Effect's HttpClient

Without Effetch, register Effect's HttpClient as your service, and call it from run:

import * as  from "effect/Effect";
import * as  from "effect/http/FetchHttpClient";
import * as  from "effect/http/HttpClient";
import * as  from "effect/http/HttpClientResponse";
import * as  from "effect/Schema";

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

const  = .({
  : .,
  : .,
  : .,
});

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

export const  = .({
  : ["todos", "detail"],
  : (: number) =>
    .(`https://api.example.com/todos/${}`).(
      .(.),
      .(.()),
    ),
  : { :  },
});
// todo(1): Query<Todo, HttpClientError | SchemaError>,
// keyed ["todos", "detail", 1]

// Sends requests with the browser's `fetch`.
export const  = .({ : . });

The query's error is whatever its Effect fails with, here Effect's HttpClientError and SchemaError, typed all the way to the component.

Use a service of your own

A query can use any service you register, such as a store that wraps your API. This file defines a list, a detail family, and a query that reads the list:

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

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

const  = .({
  : .,
  : .,
  : .,
});
type  = typeof .;

class  extends .("TodoNotFound")<{
  readonly : number;
}> {}

// Any service: an HttpClient wrapper, a database, local storage…
class  extends .<
  ,
  {
    readonly : .<<>>;
    readonly : (: number) => .<, >;
  }
>()("TodoStore") {}

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

export const  = .({
  : ["todos", "list"],
  : .(() => .),
  // How the value crosses to the browser, and how entities find todos in it.
  : { : .() },
});

export const  = .({
  : ["todos", "detail"],
  : (: number) => .(() => .()),
  : "1 minute",
});
// todo(1): Query<Todo, TodoNotFound>, keyed ["todos", "detail", 1]

// Reads the list, and runs again when the list's value changes.
export const  = .({
  : ["todos", "open-count"],
  : () =>
    .(
      .(),
      () => .(() => !.).,
    ),
});

A mutation from your own Effect works the same way, with Mutation.make, as Define a mutation shows.

What you write yourself

An endpoint gives a query its key and its schema. Without one, you write them:

  • The key. A family's key is its key followed by the argument: todo(1) is ["todos", "detail", 1]. Arguments are compared by value, so todo(1) === todo(1). Query keys explains how keys match.
  • The schema. Give a query a schema to render it on the server and to let entities find records in it. An endpoint takes it from its output. In Use a service of your own, todos has one and todo doesn't, so todo is fetched in the browser.
  • What it reads. run can read other queries through ctx.result, as openCount does. See Queries that read other queries.

Hooks, mutations, the cache and server rendering work the same as with endpoints. The parts of these docs about fromClient don't apply: keys from a route and fromClient's options. A call you make with your Effetch client still fails with the errors its statusErrors declare, as Errors by status shows for endpoints.

What's next

  • Query keys: how keys are built, and how a target matches them.
  • Infinite queries: page through a list with InfiniteQuery.make and InfiniteQuery.family.
  • Queries from a stream: make a query from a Stream with Query.stream and Query.streamFamily.
  • Query options: every option Query.make and Query.family take.

On this page