Queries
ApiQuery is the query behind a JSON:API endpoint. It needs the database feature and a registered Resource for its type.
use fusion_stack::query::{ApiQuery, Pagination};
pub fn orders() -> ApiQuery {
ApiQuery::new("orders")
.includes(["customer"])
.fields("orders", ["reference", "status", "total_cents", "currency", "created_at", "customer"])
.fields("customers", ["full_name", "email", "created_at", "orders"])
.filter("status", ["eq", "in"])
.filter("total_cents", ["gte", "lte", "gt", "lt"])
.sorts(["created_at", "total_cents", "reference"])
.default_sort("-created_at")
}
pub async fn index(request: Request) -> laravel::Result<Response> {
orders().paginate(&request).await
}Properties
PHP's query class properties are builder methods:
| PHP property | Rust method |
|---|---|
$resource | ApiQuery::new(kind) |
$includes | includes([...]) |
$fields | fields(kind, [...]), once per type |
$filters | filter(name, operators) and filter_with |
$sorts | sorts([...]) |
$defaultSort | default_sort("-created_at") |
$pagination | pagination(Pagination::Cursor) or Pagination::Offset { total } |
Includes
?include=customer adds the customer to included. Listing a nested path, such as customer.orders, allows its parents too. Paths deeper than Config::max_include_depth (3) are a 400.
Sparse fieldsets
?fields[orders]=reference,status sends only those fields of orders. Only the fields listed with fields may be asked for.
Filters
filter(name, operators) filters on the column of the same name. The operators are eq, ne, gt, gte, lt, lte and in: filter[status]=paid uses eq, filter[total_cents][gte]=1000 names the operator, and filter[status][in]=paid,shipped takes a list.
Custom filters
filter_with runs your code, given the query builder, the operator and the value:
.filter_with("search", ["eq"], |query, _, value| {
query.filter(col("orders.reference").contains(value))
})Sorts
?sort=-created_at,reference sorts by each listed column, - for descending. The key is added last to break ties. Without sort, results use default_sort, or the key.
Pagination
Cursor pagination is the default: page[size], page[after] and page[before], with links.next and links.prev. Cursors are the same as Laravel's, so they work across both packages. Pagination::Offset { total: true } uses page[size] and page[number], counts every result into meta.page.total and adds a last link. Page sizes default to 25, up to 100; change them with Config::page_sizes.
Methods
scope()
Restricts the query to the records the caller may see. It runs before filters, sorts and pagination, so no query parameter can reach past it:
.scope(|query, request| {
let account = fusion_stack::auth::client(request).map(|client| client.id).unwrap_or_default();
query.filter(col("account").eq(account))
})paginate()
One page of results as a JSON:API document, with links and meta.page.
find()
One record within the scope, or a 404 not_found.
document()
One record as the latest version's document, with the given includes, whatever the request asked for. Webhook events carry it:
orders().document(&request, id, &["customer"]).await?describe()
Describes a filter in the OpenAPI document, as #[Describe] on a PHP query class does. api:docs:lint lists filters without one.
Errors
Parameters the endpoint does not allow are a 400 with source.parameter, worded as in PHP: invalid_include, invalid_fieldset, invalid_filter, invalid_sort or invalid_page. Names in the query are the client's version's names: a client on an older version filters and sorts by the attribute names it knows.