jaybarra
Using Ash Query context in load
I have resources with a ManualRead to fetch from an external API. For all of my calls I need to pass a client that has a bearer token. I’m currently setting it on the context of the original query. I my first resource has a has_many relationship to another resource. When I try to use Ash.Query.load the second manual read doesn’t get the context, either on the query context or on the context argument.
defmodule A do
use Ash.Resource, domain: Domain
actions do
read :read do
primary? true
manual {Actions.FetchResource, key: "/resource_a"}
end
end
attributes do
uuid_primary_key :id
end
relationships do
has_many :b, B
end
end
defmodule B do
use Ash.Resource, domain: Domain
actions do
read :read do
primary? true
manual {Actions.FetchResource, key: "/resource_b"}
end
end
attributes do
uuid_primary_key :id
end
relationships do
belongs_to :a, A
end
end
defmodule Actions.FetchResource do
use Ash.Resource.ManualRead
@impl true
def read(query, _, opts, context) do
IO.inspect(query.context)
IO.inspect(context)
resource_url = Keyword.get(opts, :resource_url)
client = Map.fetch!(query.context, :client)
results = Req.request!(client, method: :get, url: resource_url).body["results']
Ash.Query.apply_to(query, xform(results)
end
end
But during usage, I can make the original query and get results, but loading the relationship fails.
A
|> Ash.Query.for_read(:read, %{}, context: %{client: client})
|> Ash.Query.limit(1)
|> Ash.read!()
This fails however, with a ** (KeyError) key :client not found in: either of the contexts for the second part of the query.
A
|> Ash.Query.for_read(:read, %{}, context: %{client: client})
|> Ash.Query.limit(1)
|> Ash.Query.load(:b)
|> Ash.read!()
How can I pass the context from the first query into the second query?
Marked As Solved
zachdaniel
Creator of Ash
You can use the shared key to pass data down.
3
Popular in Questions
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
In Ruby, I can go:
User.find_by(email: "foobar@email.com").update(email: "hello@email.com")
How can I do something similar in Elixir?
...
New
Hello everyone,
I try to use an Javascript Event Handler in my root.html.leex file.
Therefore I created a function in the app.js file: ...
New
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
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
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
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
Using vs code and installed ElixirLS: support and debugger.
And I got an error popped up on start up says
Failed to run ‘elixir’ comma...
New
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
Other popular topics
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
Hello everyone,
I try to use an Javascript Event Handler in my root.html.leex file.
Therefore I created a function in the app.js file: ...
New
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help.
Where are they similar?
Where do they differ the m...
New
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar.
I p...
New
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








