Redundant ecto queries with absinthe dataloader

Hi,

I am exploring two great libraries: absinthe and dataloader and have a small issue with the design. Here are absinthe objects of my graphql schema:

  object :topic do
    field :id, :id
    field :title, :string

    field :posts, list_of(:post), resolve: dataloader(:db)

    field :created_at, :string
    field :created_by, :user, resolve: dataloader(:db)
    field :updated_at, :string
    field :updated_by, :user, resolve: dataloader(:db)
  end


  object :post do
    field :id, :id

    field :content, :string

    field :created_at, :string
    field :created_by, :user, resolve: dataloader(:db)
    field :updated_at, :string
    field :updated_by, :user, resolve: dataloader(:db)
  end

When I fire the query below I observe 4 queries select * from users....

query {
  topic {
   title created_by { name} updated_by { name }
   posts {
      content created_by { name} updated_by { name }
   }
  }
}

Is there a way to define the absinthe objects so that there will be just one query for the users?

I managed to reduce the number of user queries by two using this “hack”

  field :updated_by, :user, resolve: dataloader(:db, :created_by)

But is there a proper way to specify a unique key for all four fields?

1 Like

To share the knowledge… :slightly_smiling_face:

I opened an issue in the dataloader github repo: Redundant ecto queries · Issue #127 · absinthe-graphql/dataloader · GitHub It contains the solution I’m using now along with other questions.

2 Likes