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
Popular in Questions
Hi guys, i’m new in the Elixir world, and i have to say, that i love it!
i’m having some problem to understand anonymous functions with ...
New
I have VueJS GUIs with the project generated using Webpack.
I have Elixir modules that will need to be used by the VueJS GUIs.
I forese...
New
What is the proper way to load a module from a file in to IEX?
In the python world, doing something like this pretty standard:
from ....
New
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
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
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
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
Other popular topics
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...
New
Hi,
I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
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
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
I am trying to run a deploy with docker and I successfully runned with this command:
docker build -t romenigld/blog-prod .
but when I t...
New
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









