florid-sojourner

florid-sojourner

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

Showing Posts 1 to 5

zachdaniel

zachdaniel

Creator of Ash

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

florid-sojourner

florid-sojourner OP

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.

zachdaniel

zachdaniel

Creator of Ash

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

florid-sojourner

florid-sojourner OP

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”
}
}

zachdaniel

zachdaniel

Creator of Ash

Sorry, my previous post had a typo :slightly_smiling_face:

filter[flow_phase][status][eq]=current would be the correct way to do it.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews