Passing values through a context

My app has a plug that has access to a struct that it wants to make available downstream, to Ash resources that use AshGraphql.

I found Ash.PlugHelpers.set_context/2, which might be the right function for the job.

I’m using AshGraphql in a resource module. It defines a :create_cart_payment_method mutation that delegates to the :create_payment_method action in the resource. In the change/2 callback of that action, the second argument is a context. At the top of the Ash.Resource.Change moduledoc, it describes, “the context, which currently only has the actor.”.

It appears these two “contexts” may not be the same thing. Nothing in the AshGraphql documentation mentions the “context” from Ash.PlugHelpers.set_context/2.

How should I pass my struct value to the change/2 function in the resource’s action, from the context that was set with Ash.PlugHelpers.set_context/2?

You need the conn to get the context value which you have previsouly set within your plug.

You can call Ash.PlugHelpers.get_context() in your controller action to retrieve the context value from the conn and then pass that returned context value into the relevant Ash resource action (or code API) as a parameter.

The value you’re looking for should show on the action being called as changeset.context or query.context.

1 Like