moxley

moxley

How to apply filtering on a relationship based on the read action of the parent

How do I apply a specific filtering on a related resource that’s two layers down from the parent resource, where the filter is determined from the parent’s read action being called?

Here’s the AshGraphql query:

query GetMember($id: ID!) {
  getMember(id: $id) {
    id
    profile {
      answers {
        id
        visible
      }
    }
  }
}

The Ash resources involved:

  • Member
    • → has_one Profile
      • → has_many Answers

For this particular GraphQL query, the answers should be filtered by visible=true, where visible is an attribute of Answer. The filtering should only apply to this particular action (:get_member), not any other actions.

Most Liked

zachdaniel

zachdaniel

Creator of Ash

Generally speaking this kind of thing is kind of difficult to do. It also causes problems for generic tooling that might think it can do things like cache any given profile that it sees in a gql response.

You could accomplish it with a relatively complex munging of the load statement in a before action hook on the action in question, which if you do go this route I can help you accomplish. You have to account for the fact that loading relationships for ash_graphql sometimes uses a calculation so that it can load the same relationship multiple ways.

We could also potentially add some kind of dedicated tool for preprocessing included relationships based on the action.

With all that said, I’d be very curious to know why you want this and if there are any other alternatives that might work for you. Resolving instances of a given type differently depending on their location in the tree is IMO an anti pattern that can cause problems later down the line.

FlyingNoodle

FlyingNoodle

Is there no way to rewrite the query like this?

query GetMember($id: ID!, $visible: Boolean!) {
  getMember(id: $id) {
    id
    profile(visible: $visible) {
      answers {
        id
        visible
      }
    }
  }
}

In that case, I assume $visible could be used as an argument somewhere? I have never used Ash with Graqphl and I’m quite new to Ash but I have the feeling that it might be easier to implement?

zachdaniel

zachdaniel

Creator of Ash

You can definitely do it out of the box, i.e something like this:

query GetMember($id: ID!) {
  getMember(id: $id) {
    id
    profile(filter: {visible: {eq: true}}) {
      answers {
        id
        visible
      }
    }
  }
}

With that said, I think that @moxley’s use case is actually quite interesting. Masquerading ought to be, I think, its own concept. I might suggest setting up an actual masquerading concept. For instance, you could add a masquerading_user calculation to your user resource that returns nil, and populate that field in a plug using a header. That header would tell your system to do something like "if the user is allowed to masquerade as the user they are requesting to masquerade as, then set the actor to %{that_user | masquerading_user: the_admin}.

Then the relevant user’s policies will automatically apply for that request.

Where Next?

Popular in Questions Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New

Other popular topics Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43806 214
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement