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
)