grangerelixx
How to use same dataloader batch key for two fields?
Hi,
I am currently trying to resolve two fields:
object :post do
field(:id, non_null(:uuid))
field :title, non_null(:string) do
resolve(&Resolvers.Blog.title_for_post/3)
end
field :category, non_null(:string) do
resolve(&Resolvers.Blog.category_for_post/3)
end
end
I have the following functions in my resolver:
def title_for_post(
post,
_,
params
) do
tenant = params.tenant
batch_key = {:one, Author, %{tenant: tenant}}
value = [post_titles: post]
Batcher.batching_fields(loader, Authors, batch_key, value)
end
def category_for_post(
post,
_,
params
) do
tenant = params.tenant
batch_key = {:one, Author, %{tenant: tenant}}
value = [post_categories: post]
Batcher.batching_fields(loader, Authors, batch_key, value)
end
Batcher.ex has the following function
def batching_fields(loader, module, batch_key, value) do
Dataloader.load(loader, module, batch_key, value)
|> on_load(fn loader ->
{:ok, loader |> Dataloader.get(module, batch_key, value)}
end)
end
And the Post dataloader currently has the following code
def data() do
Dataloader.Ecto.new(MyApp.Repo, query: &query/2, run_batch: &run_batch/5)
end
def query(Author, %{tenant: tenant}) do
Authors.get_authors(tenant)
end
def run_batch(
Author,
authors_query,
:post_titles,
posts,
_
) do
author_ids =
Enum.map(posts, & &1.author_id)
authors_map_from_query(authors_query, author_ids)
|> title_list(author_ids)
end
def run_batch(
Author,
authors_query,
:post_categories,
posts,
_
) do
author_ids =
Enum.map(posts, & &1.author_id)
authors_map_from_query(authors_query, author_ids)
|> category_list(author_ids)
end
This works but I see separate queries for title and category fields when I try to query for
query
{
posts
{
id
title
category
}
}
I would like to get a much efficient approach which is preferably using the same batch key when resolving for both fields. Any suggestions on how to achieve this?
Popular in Questions
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
Hi all,
I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage.
I’m trying to use Postgres...
New
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
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
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
Credo is smart enough to check for (something like) this:
assert length(the_list) == 0
with this response:
Checking if an enum is empt...
New
Hi everyone,
I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
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 records...
New
Other popular topics
Hi All,
I set a environment variables in dev.exs , like below code.
when i start server, how can i set the ${enable} value?
thanks.
d...
New
Hello!
tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability.
After spen...
New
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]
15:22:35.803 [error] gen_event {lager_file_backend...
New
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set?
Thanks.
New
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
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
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
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
Hi everyone,
I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








