Skuld.Query.Contract (skuld v0.30.0)

View Source

Macro for defining typed batchable fetch contracts with deffetch declarations.

A Query contract defines a set of fetch operations, generating:

  • Operation structs — nested struct modules per fetch (e.g., MyContract.GetUser)
  • Caller functions — typed public API returning computation(return_type), which suspend the current fiber for batched execution
  • Executor behaviour — typed callbacks for batch execution, one per fetch
  • Dispatch function__dispatch__/3 routes from batch key to executor callback
  • Introspection__query_operations__/0

Summary

Functions

Define a typed fetch operation.

Install multiple contract/executor pairs in one call.

Functions

deffetch(spec, opts \\ [])

(macro)

Define a typed fetch operation.

Syntax

deffetch function_name(param :: type(), ...) :: return_type()
deffetch function_name(param :: type(), ...) :: return_type(), cache: bool()

Each deffetch declaration generates an operation struct, caller function, executor callback, and dispatch clause.

with_executors(comp, pairs)

@spec with_executors(
  Skuld.Comp.Types.computation(),
  [{module(), module()}] | %{required(module()) => module()}
) :: Skuld.Comp.Types.computation()

Install multiple contract/executor pairs in one call.

Accepts either a list of {contract_module, executor_module} tuples or a map.

Examples

comp
|> Skuld.Query.Contract.with_executors([
  {MyApp.Queries.Users, MyApp.Queries.Users.EctoExecutor},
  {MyApp.Queries.Orders, MyApp.Queries.Orders.EctoExecutor}
])

comp
|> Skuld.Query.Contract.with_executors(%{
  MyApp.Queries.Users => MyApp.Queries.Users.EctoExecutor,
  MyApp.Queries.Orders => MyApp.Queries.Orders.EctoExecutor
})