jaybarra

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

zachdaniel

Creator of Ash

You can use the shared key to pass data down.

Where Next?

Popular in Questions Top

baxterw3b
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
Emily
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
Brian
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
Fl4m3Ph03n1x
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
jerry
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
Harrisonl
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
jason.o
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 Top

Qqwy
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...
3271 130579 1222
New
electic
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
minhajuddin
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
gshaw
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
romenigld
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
Harrisonl
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

We're in Beta

About us Mission Statement