Adzz

Adzz

Can I use dataloader to query for a relation of a relation?

Does anyone know if I can use dataloader to load a relation of a relation?

I have code that’s like this:

  def load_relations(
        struct_or_list,
        relations,
        %{context: %{loader: loader}},
        post_process \\ & &1
      ) do
    load_all(struct_or_list, relations, loader)
    |> on_load(fn loader ->
      Enum.reduce(relations, struct_or_list, fn relation, parent ->
        %{parent | relation => Dataloader.get(loader, :nest_db, relation, struct_or_list)}
      end)
      |> post_process.()
    end)
  end

  defp load_all(struct_or_list, relations, loader) do
    Enum.reduce(relations, loader, fn
      relation, loader ->
        Dataloader.load(loader, :nest_db, relation, struct_or_list)
    end)
  end

which allows me to do this:

    field(:thing, :thing) do
      resolve(
        load_relations( [:first_relation, :second_relation], fn thing -> ...whatever end)
     )
    end

that works but I want to turn it into this:

    field(:thing, :thing) do
      resolve(
        load_relations( [:first_relation, second_relation: [:nested_third_relation]], fn thing -> ...whatever end)
     )
    end

I’ve tried doing this:

  defp load_all(struct_or_list, relations, loader) do
    Enum.reduce(relations, loader, fn
      {relation, nested_relations}, loader ->
        load_all(
         # At this point we want the struct or list to be a different struct...
          struct_or_list,
          nested_relations,
          Dataloader.load(loader, :nest_db, relation, struct_or_list)
        )

      relation, loader ->
        Dataloader.load(loader, :nest_db, relation, struct_or_list)
    end)
  end

But that fails because of the comment above.

Am I barking up the wrong proverb?

Marked As Solved

hlx

hlx

Take a look at https://hexdocs.pm/dataloader/Dataloader.Ecto.html#module-filtering-ordering

Instead of order you could do something like preload and pick it up in your query/2

Where Next?

Popular in Questions Top

sergio
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
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
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
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
alice
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
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New

We're in Beta

About us Mission Statement