arun1

arun1

loading nested relationship in a dataloader custom resolver

I have a custom resolver in one of my graph ql types that requires all nested values to be loaded before it can run a certain calculation. I went through the dataloader documentation and github source and figured out how to use the Dataloader.load and on_load. However I cannot figure out how to load a nested relationship.

The in efficient solution is to do Repo.get inside the resolver but that creates an N+1 query problem. I know one solution is to use a batch and write a customer query but I am hoping to leverage the other fields already being loaded.

In example below, how do I load likes ?

Ecto Schema:

schema "User" do
 *** other fields ***
  has_many :posts, Module.Posts

schema "Posts" do
   has_many :likes,  Module.Likes

schema "Likes" do
  field :type, :string

Absinthe Type Definition:

object :like do
 field(:type, :string)
end

object :posts do 
  field(:likes, :like, resolve: dataloader(Module)
end

object :users do
    *** other fields ***
 field(:posts, :post, resolve: dataloader(Module))
    )

 field(:some_calc_value, :integer,
      resolve: fn parent, _, %{context: %{loader: loader}} ->
        loader
        |> Dataloader.load(Module, :posts, parent)
        |> Dataloader.load ( ????????)  // HOW DO I LOAD LIKES FOR EACH POST ?
        |> on_load(fn loader ->
         posts = 
          loader
          |> Dataloader.get(Module, :posts, parent)

         likes = ???? How DO I Get the likes ? 

          calculate_value(posts, likes)
          |> (&{:ok, &1}).()
        end)
      end
    )

Where Next?

Popular in Questions Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested 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
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
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
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
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
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
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
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

We're in Beta

About us Mission Statement