Skip to content

Queries ​

ApiQuery is the query behind a JSON:API endpoint. It needs the database feature and a registered Resource for its type.

rust
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 propertyRust method
$resourceApiQuery::new(kind)
$includesincludes([...])
$fieldsfields(kind, [...]), once per type
$filtersfilter(name, operators) and filter_with
$sortssorts([...])
$defaultSortdefault_sort("-created_at")
$paginationpagination(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:

rust
.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:

rust
.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:

rust
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.

Released under the MIT License.