Ash Graphql "create" mutation with upsert? true – how to use ID as argument for upsert identity?

I have a Graphql create mutation within which upsert? true and upsert_identity :id. I have an action corresponding to this (code below). Can I get id to be an acceptable input to this mutation? Thanks!
mutations do
create :upsert_patient, :upsert_patient, upsert?: true, upsert_identity: :id
end
mutations do
create :upsert_patient, :upsert_patient, upsert?: true, upsert_identity: :id
end
create :upsert_patient do
upsert? true
upsert_identity :id
end
create :upsert_patient do
upsert? true
upsert_identity :id
end
3 Replies
shyan
shyanOP2y ago
And to clarify – if the id is provided and does not match an existing record – I want that to result in an error. If the id is not provided at all, I want that to result in a "create". And if the id is provided and does match an existing record, I want that to result in an "update". What's the recommended way of going about doing this? I initially had two separate mutations for two separate actions (create and update) but when I saw upsert? true was an option – I wanted to take advantage of it.
ZachDaniel
ZachDaniel2y ago
🤔 I think you need to do something a bit custom for that
create :upsert_if_id_not_specified do
argument :id, :uuid

manual fn changeset, _ ->
case Ash.Changeset.fetch_argument(changeset, :id) do
{:ok, id} ->
lookup_and_update()
:error ->
call_upsert_action()
end
end
end
create :upsert_if_id_not_specified do
argument :id, :uuid

manual fn changeset, _ ->
case Ash.Changeset.fetch_argument(changeset, :id) do
{:ok, id} ->
lookup_and_update()
:error ->
call_upsert_action()
end
end
end
shyan
shyanOP2y ago
Rad thank you Zach, I'm going to try it out

Did you find this page helpful?