BartOtten

BartOtten

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

Showing Posts 1 to 10

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 OP

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

Yeah I mean the issue with lateral joins is that while they’re sort of possible with Ecto, they require writing out the lateral join as a string, which makes it very difficult to do in a generic way inside a library.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe
BartOtten

BartOtten OP

Had a weekend without a screen (almost, it’s 23.18) but will have a good look at it tomorrow :slight_smile:

Have a working ‘first: x’ implementation (which indeed is not generic) but have to resolve ordering in the lateral join (using ecto, pure postgres returns in correct order).

@benwilson512 Why support Relay while this ain’t much of a possibility? Answer might be in you book but couldn’t read any further today (ereader doesnt count as a screen (a))

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

I’m not sure I understand this. Absinthe Relay encompasses a variety of functionality, the fact that Ecto makes it hard to do efficient connections inside connections, itself a relatively uncommon access pattern, doesn’t negate all of that.

BartOtten

BartOtten OP

Sorry, was to tired to actually grab my phone to remove the comment once I realized I was 100% off with my question. Although your answer raises another question as you say connections inside connections, itself a relatively uncommon access pattern. Why is this relatively uncommon (googled, didn’t find a thing)? In your book you give examples of the advantage of single Graphql call over multiple REST calls. As far I can see right now, doing connections in connections is the way to reach the single call. For example:

recipes >- ingredients → ingredients >- in_stock → stores >- addresses → addresses

Of course I could split those, and query for this information ‘just in time’ while navigating in the (web)app. But that seems to undo a little bit of the purpose of Graphql.

dantswain

dantswain

Hey @benwilson512 I’m curious if this story has changed any in the last 4 years? IIUC Ecto support for lateral joins and window queries has improved quite a bit and I’m presently looking at a project where I might be able to leverage AbsintheRelay.

maxs

maxs

@benwilson512 here’s a great example of how easy it is to do it in Ecto, would love to know how to get this to work with Dataloader:

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Hey folks, lateral joins have been supported in Dataloader for a while now, which means that limits should work just fine!

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews