sezaru

sezaru

Argument derived from struct for manage_relationship changes

When I want to add an manage_relationship change to a create/update action, all the examples I see use an argument for it with :map

update :something do
  argument :bla, :map

  change manage_relationship(:bla, type: :create, on_no_match: :create)
end

This works, but it makes create apis a little bit cumbersome since :bla can be a map with anything inside.

For example, using AshTypescript in the above action, will make :bla argument be any instead of a proper typed class.

I tried doing something like this:

update :something do
  argument :bla, :struct do
    constraints instance_of: Bla
  end

  change manage_relationship(:bla, type: :create, on_no_match: :create)
end

This will generate the correct type, but, it will fail because it will expect now that my :bla argument contains :id, :inserted_at and :updated__at

I’m wondering, is there a proper way to do this? I could keep it a map and add constraints for the keys, but that just means that every time I change Bla, I need to remember that there is this action that I need to update the input too.

Basically, what I think this should do is if I use the struct, it would filter the resource attribute for only its writable attributes and use this as the argument possible keys.

For example:

# This one contains :id, :inserted_at and :updated_at, so, it doesn't work
iex(4)> Bla |> Ash.Resource.Info.attributes() |> Enum.map(& &1.name)
[:id, :licenses, :units_sold_last_12_months, :amount_sold_last_12_months,
 :inserted_at, :updated_at, :user_id]

# This one filters not writable fields, so it should work
iex(3)> Bla |> Ash.Resource.Info.attributes() |> Enum.filter(& &1.writable?) |> Enum.map(& &1.name)
[:licenses, :units_sold_last_12_months, :amount_sold_last_12_months, :user_id]

First Post!

zachdaniel

zachdaniel

Creator of Ash

This is actually something that we do in AshGraphql that we can port to AshTypescript. It is fairly complex, but a managed relationship can call multiple actions theoretically so building their input types is complex. I’d suggest opening an issue in AshTypescript and expressing the value and explaining that there is code in AshGraphql to use as a reference.

Last Post!

sezaru

sezaru

Do you know which file implements that in AshGraphql so I can reference it directly in the ticket?

Where Next?

Popular in Questions Top

hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
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
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
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
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
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
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54921 245
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
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New

We're in Beta

About us Mission Statement