Skip to Content
Effetch Query
Reference

@monarcode/effetch-query/effetch

Every export of the Effetch entry point: queries and mutations from Effetch endpoints.

@monarcode/effetch-query/effetch turns Effetch endpoints into queries and mutations. Import what you use:

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

Its exports follow, grouped by the guide that covers them, each with its docs. Your editor shows the same docs, with an example, when you hover the name.

Queries from endpoints

Guide: Queries from endpoints.

fromClient

Turns Fetch.endpoint definitions into queries and mutations that call them through your Effetch client: give it the client's service, a Fetch.Service class or a Context.Service key. Use it at module level, next to the definitions: the client is only reached when a query fetches or a mutation runs, from your services (so the service must be in Register["services"]). Returns an Endpoints, whose query, infiniteQuery and mutation make them.

The adapter adds no behavior of its own: a query is a Query.family member keyed by its route, and a mutation is a Mutation.make whose run calls the endpoint, and that invalidates its route with EndpointsOptions.invalidates. From a Fetch.Service class it reads the defaultError and statusErrors the client is made with, to send errors to the browser; from a Context.Service key, give them in the options (EndpointsOptions). The definitions of a Fetch.Endpoints class (TodoApi.definitions) work as any other. It needs @monarcode/effetch 0.1, from 0.1.0-rc.4.

endpoints

Deprecated. Use fromClient: rename the import and the call.

The former name of fromClient, which works the same: it takes the same client and options, and returns the same Endpoints. It stays for the 0.1 releases and goes in 0.2.0.

EndpointQuery

A family of queries, one per call of an Effetch endpoint, made by Endpoints.query. Call it with the endpoint's params and query to get the query for that call. Its key is the route's path with the params filled in, then the query object: GET /todos/:id called with { params: { id: 1 } } is ["todos", 1].

Calls that send the same request share a query: what isn't sent doesn't count (params the path doesn't name, null and undefined query values and array items, and arrays left empty). Without a query schema, values are otherwise compared as given, so { page: 1 } and { page: "1" } are two queries.

The key has no method, scheme or host, so give a backend of its own a prefix (EndpointsOptions.key), and make one EndpointQuery per endpoint: two would share keys but not their cache.

As a target of Query.invalidate, Query.refetch or a mutation's invalidates, it matches exactly its own queries, not other routes under the same path.

With a query schema on the endpoint (QS), query is typed by the schema, and the key holds the query as the schema encodes it, as it's sent: each value a string (a Date as its ISO string, 2 as "2") and each field by its sent name. Equal requests share a query, and keys stay JSON for server rendering. The query a member was first called with is the one it sends, and the args Query.update gives. A call throws an EndpointQueryError when its query doesn't match the schema, a field encodes to an object, or a field is Redacted: a key would hold the secret and send it to the browser, so send it in a header.

Prop

Type

EndpointQueryArgs

What a query from route R takes: the path's params, and a query object. They make its key, so headers and plugins are given once, in EndpointQueryOptions, instead.

  • Q: the type of query, checked with Fetch.QueryOf, so a value typed with an interface is accepted too. Default: Fetch.QueryInput.
  • QS: the endpoint's query schema, if it has one: query is then the schema's type, required when the schema has required fields.

EndpointQueryCallError

The error of a query Endpoints.query makes from an endpoint: the Fetch.CallError of its call, from its output schema O, its error body schema EB (ErrorSchema), the client's plugins Ps, its status errors SE and its stream format St. A stream endpoint's elements are decoded as they come, so its DecodeError isn't for the whole output.

EndpointQueryOptions

Options for Endpoints.query: a query's options (Query.Options), a placeholder, live updates, and the headers and plugin overrides client.bind takes (Fetch.BindOptions). A is the query's value and Args the call's params and query (EndpointQueryArgs), which type placeholder and live; api.query sets both from the endpoint.

Prop

Type

EndpointQueryOptionsFor

The options Endpoints.query takes for an endpoint: those of a stream query (EndpointStreamOptions) for a stream endpoint, whose stream format St is "sse" or "ndjson", else those of a fetched one (EndpointQueryOptions). E is the query's error (EndpointQueryCallError), A its value (EndpointQueryValue), R the endpoint's route and QS its query schema, which type placeholder and live.

EndpointQueryValue

The value of a query Endpoints.query makes from an endpoint, from its output schema O and its stream format St: the decoded output (the raw HttpClientResponse without one), or for a stream endpoint (stream: "sse" or "ndjson") its latest element, a Fetch.SseEvent of the output for SSE.

Endpoints

Queries and mutations for one Effetch client, made by fromClient.

Prop

Type

EndpointsOptions

Options for fromClient.

Prop

Type

RegisteredSchema

A schema whose decoding and encoding need nothing outside your registered services (Register["services"]) and the client. The adapter accepts only these, so an endpoint whose schema needs another service is a compile error where its query or mutation is defined (Endpoints.query, Endpoints.mutation).

Query keys

Guide: Query keys.

EndpointQueryError

Thrown when a call's query can't be part of its key: it doesn't match the endpoint's query schema, a field encodes to an object, or a field is Redacted, whose secret the key would hold and send to the browser. Send a secret in a header instead. It's thrown when the query is made, such as todos({ query }) in a component, before anything is fetched.

KeyQuerySchema

A query schema an endpoint query can be keyed by: one whose encoding needs no services, since a key is encoded when the family is called, outside any Effect. Endpoints.mutation takes any query schema whose services are registered.

Query errors

Guide: Query errors.

ErrorSchema

The error schema a bound endpoint decodes error bodies with: its own, or the client's defaultError (see EndpointsOptions.defaultError).

Errors by status

Guide: Query errors.

RegisteredStatusErrors

A statusErrors map (Fetch.StatusErrorMap) whose schemas need nothing outside your registered services, as RegisteredSchema. The adapter accepts only these, on the client and on each endpoint: its queries and mutations decode the errors, and its queries send them to the browser (the client's only when fromClient has them, see EndpointsOptions.statusErrors).

Queries from a stream

Guide: Queries from a stream.

EndpointStreamOptions

Options for Endpoints.query with a stream endpoint (stream: "sse" or "ndjson"): a stream query's options (Query.StreamOptions), a placeholder, and the headers and plugin overrides client.bind takes (Fetch.BindOptions). A is the value, one element of the stream, and Args the call's arguments.

Prop

Type

Infinite queries

Guide: Infinite queries.

EndpointInfiniteQuery

A family of infinite queries, one per call of an Effetch endpoint, made by Endpoints.infiniteQuery. Call it with the endpoint's params and query to get the InfiniteQuery.InfiniteQuery for that call; a page field given there is ignored. Keys and matching are as for EndpointQuery.

Prop

Type

EndpointInfiniteQueryOptions

Options for Endpoints.infiniteQuery: an infinite query's page options (InfiniteQuery.PageOptions) and query options (Query.Options), the query field each page's param is sent in, and the headers and plugin overrides client.bind takes (Fetch.BindOptions).

Prop

Type

EndpointPageField

The query fields an infinite query's EndpointInfiniteQueryOptions.pageParam can name. The param P must be a Fetch.QueryValue, such as a string or a number, so pages cross to the browser as JSON. Without a query schema (QS is undefined), any field. With one, the schema's optional fields whose type takes P (undefined aside, for a first page with no cursor): optional, so a call can leave it out.

Define a mutation

Guide: Define a mutation.

EndpointMutationOptions

Options for Endpoints.mutation: a mutation's options (Mutation.MakeOptions) without run, which is the endpoint, and the headers and plugin overrides client.bind takes (Fetch.BindOptions).

Prop

Type

EndpointVariables

What a mutation from route R takes when run: the endpoint's call options (params, query, body, headers, plugins), or nothing when none are required. The variables of Endpoints.mutation.

Without a query schema (QS), its query is a Fetch.QueryInput, which has an index signature: type a query object you pass with a type alias rather than an interface. With one, it's the schema's type.

On this page