-
Notifications
You must be signed in to change notification settings - Fork 525
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dataloader helper for custom run_batch #903
Comments
I think I'd like to avoid a tuple based approach, Dataloader already has a lot of tuple formats. Maybe a map? eg:
|
I'm not against this, I agree tuples can be confusing, especially when you include the optional arguments that can be passed ( |
@Gazler good point, maybe simply |
Fixed per PR |
Hey, just tried that and i'm super confused, whats the final syntax? where is it documented? |
Given the example in the first post the following should work with the new syntax: # your object definition
resolve dataloader(Posts, fn user, _args, _ ->
%{batch: {{:one, Post}, %{}}, item: [post_count: user]}
end) The empty map passed in the tuple for the |
Is there a way to make the absinthe |
@VictorGaiva data about the current user should be passed in as part of the args tuple. eg:
|
@benwilson512 With: object :product do
field :following, non_null(:boolean), default_value: false do
resolve(
dataloader(Db, fn product, _args, %{context: %{current_user: user}} ->
%{batch: {{:one, Core.User}, %{user: user}}, item: [following: product]}
end)
)
end
end and def run_batch(a, b, :following, products, c) do
IO.inspect(a)
IO.inspect(b)
IO.inspect(c)
# result = Products.followed_by() |> Enum.map(& &1.id)
for %{id: _id} <- products do
[false]
end
end prints:
And |
Was able to do what I wanted, inspired by this forum post, like this: With: object :product do
field :following, non_null(:boolean), default_value: false do
resolve(
dataloader(Db, fn product, _args, %{context: %{current_user: user}} ->
%{batch: {:following, user}, item: product}
end)
)
end
end and def load({:following, user}, products) do
result = Products.followed_by(user) |> Enum.map(& &1.id)
products
|> Map.new(fn %{id: id} = product ->
{product, Enum.member?(result, id)}
end)
end |
Currently when using a custom
run_batch/5
function such as the one from the docs:https://hexdocs.pm/dataloader/Dataloader.Ecto.html#module-custom-batch-queries
In order to execute the from a resolver, currently, it requires a resolver like:
It is possible today to us a helper like this for resolving:
However there is no way to express the required
item_key
for the dataloader.Perhaps the dataloader response could be altered to permit this. Maybe something like:
The text was updated successfully, but these errors were encountered: