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

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
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
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
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
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

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 131117 1222
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
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
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement