florid-sojourner
Omitting nillable filter arguments returns no results
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
First Post!
zachdaniel
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
Last Post!
zachdaniel
Sorry, my previous post had a typo ![]()
filter[flow_phase][status][eq]=current would be the correct way to do it.
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #elixirconf-us
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










