Skuld.Query (skuld v0.30.0)

View Source

Primary API for query contracts, wiring, and caching.

Defining a query contract

defmodule MyApp.Queries do
  use Skuld.Query

  deffetch get_user(id :: String.t()) :: User.t() | nil
  deffetch get_orders(user_id :: String.t()) :: [Order.t()]
end

Wiring executors

comp
|> Skuld.Query.with_executor(MyApp.Queries, MyApp.Queries.EctoExecutor)
|> FiberPool.with_handler()
|> Comp.run()

With caching

comp
|> Skuld.Query.with_cached_executor(MyApp.Queries, MyApp.Queries.EctoExecutor)
|> FiberPool.with_handler()
|> Comp.run()

Summary

Functions

Map a function over items, running each result as a fiber so that deffetch calls within each iteration batch together.

Install a caching-wrapped executor for a single contract. Shorthand for with_cached_executors/2.

Install caching-wrapped executors for multiple contracts. Initialises a scoped cache and wraps executors so identical queries return cached results without re-executing.

Install an executor module for a single query contract.

Install executor modules for multiple query contracts. Accepts either a list of {contract_module, executor_module} tuples or a map.

Functions

map(items, fun)

Map a function over items, running each result as a fiber so that deffetch calls within each iteration batch together.

Requires FiberPool.with_handler in the stack. Results are returned in the same order as the input items.

Example

details <- Query.map(order_ids, &AccountQueries.fetch_order_details/1)

with_cached_executor(comp, contract_module, executor_module)

Install a caching-wrapped executor for a single contract. Shorthand for with_cached_executors/2.

with_cached_executors(comp, pairs)

Install caching-wrapped executors for multiple contracts. Initialises a scoped cache and wraps executors so identical queries return cached results without re-executing.

with_executor(comp, contract_module, executor_module)

Install an executor module for a single query contract.

The executor module must implement the contract's callbacks.

with_executors(comp, pairs)

Install executor modules for multiple query contracts. Accepts either a list of {contract_module, executor_module} tuples or a map.