SqlResolver
Schema-aware RequestResolver helpers for SQL-backed data loading.
This module represents each lookup or mutation as a SqlRequest and batches concurrent requests into SQL operations. Request payloads are encoded with the request schema before execute is called, and returned rows are decoded with the result schema before requests are completed. It provides ordered, grouped, id-based, and side-effect-only resolver constructors, and keeps batches separated by the active SQL transaction connection.
Constructors
SqlRequest
Signature
declare const SqlRequest: <In, A, E, R>(payload: In) => SqlRequest<In, A, E, R>;Models
SqlRequest interface
Request type used by SQL request resolvers, carrying the input payload together with the resolver's result, error, and environment types.
Signature
interface SqlRequest<In, A, E, R> extends Request<A, E | Schema.SchemaError, R> {
readonly payload: In;
}Other
Signature
declare function void<Req extends Constraint, _, E, R>(options: {
readonly execute: (requests: [Req["Encoded"], ...Array<Req["Encoded"]>]) => Effect<readonly Array<_>, E, R>;
readonly Request: Req;
}): RequestResolver<SqlRequest<Req["Type"], void, SchemaError | E, R | Req["EncodingServices"]>>Resolvers
Creates a batched resolver that fetches rows for encoded ids, decodes results, completes each matching request using ResultId, and fails missing ids with NoSuchElementError.
Signature
declare function findById<Id extends Constraint, Res extends Constraint, Row, E, R>(options: {
readonly execute: (requests: [Id["Encoded"], ...Array<Id["Encoded"]>]) => Effect<readonly Array<Row>, E, R>;
readonly Id: Id;
readonly Result: Res;
readonly ResultId: (result: Res["Type"], row: NoInfer<Row>) => Id["Type"];
}): RequestResolver<SqlRequest<Id["Type"], Res["Type"], NoSuchElementError | SchemaError | E, R | Id["EncodingServices"] | Res["DecodingServices"]>>Creates a batched SQL request resolver that encodes requests, decodes result rows, groups decoded results by matching request and result keys, and fails a request with NoSuchElementError when no result group exists.
Signature
declare function grouped<Req extends Constraint, Res extends Constraint, K, Row, E, R>(options: {
readonly execute: (requests: [Req["Encoded"], ...Array<Req["Encoded"]>]) => Effect<readonly Array<Row>, E, R>;
readonly Request: Req;
readonly RequestGroupKey: (request: Req["Type"]) => K;
readonly Result: Res;
readonly ResultGroupKey: (result: Res["Type"], row: NoInfer<Row>) => K;
}): RequestResolver<SqlRequest<Req["Type"], [Res["Type"], ...Array<Res["Type"]>], NoSuchElementError | SchemaError | E, R | Req["EncodingServices"] | Res["DecodingServices"]>>Creates a resolver for a SQL query with a request schema and a result schema.
Details
The request schema is used to validate the input of the query, and the result schema is used to validate the output of the query. Results are mapped to the requests in order, so the length of the results must match the length of the requests.
Signature
declare function ordered<Req extends Constraint, Res extends Constraint, _, E, R>(options: {
readonly execute: (requests: [Req["Encoded"], ...Array<Req["Encoded"]>]) => Effect<readonly Array<_>, E, R>;
readonly Request: Req;
readonly Result: Res;
}): RequestResolver<SqlRequest<Req["Type"], Res["Type"], ResultLengthMismatch | E, R | Req["EncodingServices"] | Res["DecodingServices"]>>Running
Runs a payload as a SqlRequest through a request resolver, either directly with a payload and resolver or curried by resolver.
Signature
declare const request: {
<In, A, E, R>(
resolver: RequestResolver<SqlRequest<In, A, E, R>>,
): (payload: In) => Effect<A, SchemaError | E, R>;
<In, A, E, R>(
payload: In,
resolver: RequestResolver<SqlRequest<In, A, E, R>>,
): Effect<A, SchemaError | E, R>;
};
Constructs a
SqlRequestfrom a payload. Equality and hashing are based on the payload so equal requests can be batched and deduplicated.