Hi!
I was under the impression that Ash’s context “travels” along when managing relationships within actions. It seems not to be the case, is there a way to achieve this?
Here’s a simplified example of what I’m trying to do:
Let’s say we have Parent
and Child
resources. Parent has an update action refresh
, and Child has the same action. We want any refresh on a Child to trigger the refresh action on the Parent. And we want the context provided to a Child’s action to be visible in the Parent’s action called via manage_relationship
.
So I’ve tried something like this:
Ash.update!(a_child, %{some: :param}, action: :refresh, context: %{very: %{large: "map"}})
# ...
defmodule Child do
use Ash.Resource # etc
actions
update :refresh do
# arguments etc...
change Child.Changes.Refresh
end
end
end
defmodule Parent do
use Ash.Resource # etc
actions
update :refresh do
# arguments etc...
change Parent.Changes.Refresh
end
end
end
defmodule Child.Changes.Refresh do
use Ash.Resource.Change
import Ash.Changeset
@impl true
def change(changeset, _opts, _context) do
dbg(changeset.context) # the pushed context is here
payload = %{document_id: get_attribute(changeset, :parent_id)}
manage_relationship(:parent, payload, on_match: {:update, :refresh})
end
end
defmodule Parent.Changes.Refresh do
use Ash.Resource.Change
import Ash.Changeset
@impl true
def change(changeset, _opts, _context) do
dbg(changeset.context) # the pushed context is not here...
# do something else with
changeset
end
end
I guess I could solve this but adding this context
as an argument to Parent’s refresh
action. But am I missing something, is context not as “global” as I expected?
Thanks in advance for taking a look at this.