BartOtten
How to batch load with Absinthe Relay while supporting first: x and after_cursor?
I’m trying to use Absinthe Relay to create Relay compatible Graphql endpoint. I am having troubles to create connections with batching.
Trying to query this, which per organization the two youngest users:
query ConnectionOrganizationListPaginationQuery([...]) {
organizations(first: 10 etc....) {
edges {
node {
name
users(first: 2, orderBy: {age: ASC}) {
edges {
node {
name
age
}
[...Cursors and PageInfo...]
}
The example below works:
In schema
connection field :users, node_type: :user do
arg(:search_for, list_of(:user_search), description: "List of search objects")
arg(:order_by, list_of(:user_order), description: "List of order objects")
arg(:filter_at, list_of(:user_filter), description: "List of filter objects")
resolve fn
pagination_args, %{source: organization} ->
args = Map.merge(pagination_args, %{filter_at: [%{organization_id: ["#{organization.id}"]}]})
UsersResolver.list(args, %{})
end
end
In resolver
def list(args, _) do
My.App.Users.Repository.list(args, %{role: :stub_admin})
|> Connection.from_query(&Repo.all/1, args)
end
This leads to the infamous N=1 problem. Using field :users, list_of(:user), resolve: dataloader(My.App.Users) works for batching, but doesn’t seem to be compatible with Absinthe Relay. I just can’t find the right way to do batch loading compatible with Absinthe Relay. Is it possible at all? If so, please provide a few hints or an example ![]()
ps. I rather not load the whole list of all users in memory for pagination.
pps. Yes, I have searched myself ![]()
![]()
Most Liked
benwilson512
The short version is: I don’t know.
The longer version is that if you look at this from a pure SQL perspective, the problem we’re trying to solve is this: Given a set of posts, how do you load the last 10 comments from each post. This is possible to do, but it requires either lateral joins or window functions, neither of which are easily supported by Ecto.
BartOtten
Thank you Ben, I already took the lateral join path. Feels much better to know it’s hard and it’s not just me writing unnecessary complex code.
I will post the solution here..if I find one.
benwilson512
For a comprehensive breakdown of how difficult this is see: How to address the issue with limits on preloaded associations using Ecto? - #8 by peerreynders
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








