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
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
3
Popular in Questions
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!
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
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
I have a User schema with a :from_id field set to type :string:
defmodule TweetBot.Repo.Migrations.CreateUsers do
use Ecto.Migration
...
New
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
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
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size?
Thanks
New
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
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
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
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
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
Manning 2016 Halloween weekend sale via Deal of the Day
Friday, October 28 - Half off all MEAPs - code WM102816LT
Saturday, October 29 ...
New
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
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
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
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...
New
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
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
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New








