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
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
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
.
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
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
Popular in Questions
Other popular topics
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









