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 https://github.com/absinthe-graphql/dataloader/pull/93 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!

Francesco

Francesco

Sorry for the slow response! @mbuhot I’ve just modified the code to ensure the query used to load the association always runs in a lateral join but I don’t really know enough to know if it is correct.

Why did you only apply the lateral join subquery when we detected the presence of limit? Is it safe to run it for every query type?

Where Next?

Popular in Questions Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
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
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
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
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
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
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
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

Other popular topics Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
joaquinalcerro
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 record...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
baxterw3b
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

We're in Beta

About us Mission Statement