How do I filter an action by multiple arguments but allow one or more of the arguments to be nil, and in those cases, drop that filter clause?
In other words, with what I’ve written, all the arguments are needed or no results are returned.
localhost:4000/api/flows?inlclude=project&include=phase&include=flow_phase&owned_by=guild&status=active&phase_status=current
returns results, but
localhost:4000/api/flows?inlclude=project&include=phase&include=flow_phase&owned_by=guild&status=active
returns nothing. My desire is that it would have returned MORE results.
I’m relatively new to both Ash and Elixir, so please forgive me if the answer should be obvious.
Here’s my action:
actions do
defaults [:create]
read :read do
primary? true
prepare(build(load: [:project]))
argument :status, :atom do
allow_nil? true
end
argument :owned_by, :atom do
allow_nil? true
end
argument :phase_status, :atom do
allow_nil? true
end
filter expr(
status == ^arg(:status) and flow_phase.status == (^arg(:phase_status)) and
flow_phase.phase.owned_by == ^arg(:owned_by)
)
end
end
This can be done in the expression with a statement like
filter expr(
(is_nil(^arg(:status) || status == ^arg(:status)) and (is_nil(^arg(:phase_staus) || flow_phase.status == (^arg(:phase_status))) and
(is_nil(^arg(:phase_status)) || flow_phase.phase.owned_by == ^arg(:owned_by)
))
With that said, if you’re using ash_json_api, a filter can already be provided as part of the api, so I’d suggest using that vs hand rolling your own filter interface
Thanks!
I would love to use the Ash JSON API filtering. I can get a basic filter to work, but not a filter through a relationship.
The JSON API documentation says that the spec is agnostic about the filtering strategy implemented by a server. JSON:API — Recommendations Is there a clear set of documentation for filtering using Ash JSON API? I’m struggling to locate any.
Unfortunately I don’t think we have a detailed documentation of the filter format. With that said, you can filter through relationships like so filter=relationship[field][eq]=10
, for example. This spec is well formed for this, just not well documented. What I would really like to do is properly create the json schema for a filter, in the same way we properly type out the filter type for ash_graphql
(users of which don’t have this problem since the whole thing is typed).
I’m participating in evaluating Ash in an active project at my organization and I’m hopeful we can adopt it. The documentation situation is unfortunate but I also understand. Your time is limited.
I’m using this query:
localhost:4000/api/flows?inlclude=project&include=phase&include=flow_phase&filter=flow_phase[status][eq]=current
And getting this error:
{
“errors”: [
{
“code”: “FrameworkError”,
“id”: “bf898af0-17ee-4fe3-9325-bdfc57d1ad0f”,
“status”: “500”,
“title”: “Framework Error”,
“detail”: “Returned when an unexpected error in the framework has occurred.”
},
{
“code”: “InvalidQuery”,
“id”: “1588e20a-e946-4eb7-ae50-0f28f45ab121”,
“status”: “400”,
“title”: “Invalid Query”,
“source”: {
“parameter”: “filter”
},
“detail”: “Expected "object", got "flow_phase[status][eq]=current", at ["filter"].”
}
],
“jsonapi”: {
“version”: “1.0”
}
}
Sorry, my previous post had a typo
filter[flow_phase][status][eq]=current
would be the correct way to do it.