BartOtten

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 :slight_smile:

ps. I rather not load the whole list of all users in memory for pagination.
pps. Yes, I have searched myself :wink:
image

Most Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

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

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

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Where Next?

Popular in Questions Top

siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
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
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
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
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
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 records...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement