moxley

moxley

Need to return `nil` for a belongs_to relationship value based on criteria

I’m building an AshGraphql query that should return null for a belongs_to field if the actor doesn’t have permission to access that field, rather that raising a policy violation from the child resource. Right now, there is a policy violation when there is a read attempt on the belongs_to field. This actually isn’t the behavior I need. I need to the field to simply be nil if the user doesn’t have the right privileges.

I tried the the filter option to belongs_to, but it doesn’t appear to prevent reading the related field, even if the expression is expr(false). The policy violation on the related resource is still triggered, failing the parent read.

defmodule GF.Events.Event2 do
  # ...

  relationships do
    belongs_to :venue, GF.Events.Venue2 do
      attribute_type :integer
      public? true
      # Want to restrict access based on actor criteria, but even a `false` expression doesn't prevent a read to Venue2
      filter expr(false)
    end
  end
end

How can I have the belongs_to value be nil for certain criteria when the child record actually exists, without raising a policy violation on the child resource/

Marked As Solved

zachdaniel

zachdaniel

Creator of Ash

So this is something that we don’t actually have at the moment. When requesting related data currently the value is either provided or the entire request is forbidden. However, this is a feature that we can add at the framework level likely without too much fuss. We’d have to make it configurable, perhaps globally, perhaps on a relationship-by-relationship basis. This would also change the types of to_many relationships because they could return a list or nil (plus errors).

In the meantime what you could do is write a calculation, and then use field policies on that calculation.

Also Liked

moxley

moxley

That makes sense. I’ll do a calculation, which is already being done for several other fields on this resource. Thanks!

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New

Other popular topics Top

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
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement