Skip to Content
Effetch Query
Queries

Queries without Effetch

Define queries and mutations from any Effect, without Effetch.

Any Effect can be a query, so Effetch Query works without Effetch: with Effect's own HttpClient, a database or local storage. Query.make defines one query, Query.family one per argument, and Mutation.make a mutation, each with an Effect you write. Register the services they use, as in 1. Describe your API, and give the client a layer that provides them.

With Effect's HttpClient. Register 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>

// 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.

With a service of your own. A query can use any service you register, such as a store that wraps your API. This file defines queries, a query that reads another and a mutation with an optimistic update:

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) => .<, >;
    readonly : (
      : number,
      : boolean,
    ) => .<, >;
  }
>()("TodoStore") {}

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

export const  = .({
  : ["todos", "list"],
  : .(() => .),
  // Only for server rendering: how the value crosses to the browser.
  : { : .() },
});

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

// A query can read another one, and refetches when it changes.
export const  = .({
  : ["todos", "open-count"],
  : () =>
    .(
      .(),
      () => .(() => !.).,
    ),
});

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

The difference from endpoint queries is what you write yourself:

  • Keys are yours to write. 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).
  • run can read other queries through ctx.result, as openCount does (Queries that read other queries).
  • To render on the server, give the query a schema. Effetch's endpoints don't need one, since their output is it.
  • InfiniteQuery.make and InfiniteQuery.family take a run from the page's param, (args, pageParam) => Effect for a family, with the paging options. Query.stream and Query.streamFamily take a Stream (Queries from a stream).

Everything else in these docs, hooks, mutations, the cache and server rendering, works the same.