Francesco

Francesco

How can I limit the number of results returned by Dataloader?

Hello!

I’m using Absinthe + Dataloader and I have a particular field that returns a lot of results (1000s). I would like to cap it to 10 results, but I’m not sure how since if I did something like the following then it would limit it to 10 results total, not 10 results per parent field.

  def query(View = view, args) do
    view
    |> limit(10)
    |> tablet_query(args)
  end

One way I can think of is to use a custom resolver with dataloader.load and do the filtering in elixir and not with an ecto query. However this seems redundant, is there any way to go about implementing this limiting logic in an Absinthe query?

Thanks in advance for the help!

Marked As Solved

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Thanks to heroic efforts by @mbuhot Support limit queries with Dataloader.Ecto by mbuhot · Pull Request #93 · absinthe-graphql/dataloader · GitHub limit in dataloader actually behaves the way you want! Just make sure to update to 1.0.8 which I just released.

Also Liked

mbuhot

mbuhot

The implementation is currently checking for the presence of a limit in the query.

When a limit is applied, the query used to load the association is run in a lateral subquery, which would have the effect of applying the distinct separately to each parent :+1:.

If you’d like to send a PR that ensures distinct queries are always applied per-parent, the code that needs updating is: here and here

Francesco

Francesco

Amazing, that’s great to hear! As far as a pattern for implementing this goes, would you do something like the following:

    field :views, list_of(:view) do
      arg(:limit, type: :integer)
      arg(:order, type: :sort_order, default_value: :desc)

      resolve(dataloader(Scribe.Analytics))
    end

then in Scribe.Analytics (the context module) have:

  def query(View, args) do
    view_query(args)
  end

  defp view_query(args) do
    Enum.reduce(args, View, fn
      {:order, order}, query ->
        query |> order_by({^order, :inserted_at})
      {:limit, limit_to}, query ->
         query |> limit(limit_to)
    end)
  end
benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

I think that should work fine!

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
greenz1
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
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

sen
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
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
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
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
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
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
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
boundedvariable
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
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New

We're in Beta

About us Mission Statement