Skip to content

Redundant ecto queries #127

Open
Open
@sam701

Description

@sam701

I'm trying to plug this awesome dataloader module. It's working as expected so far but I'm observing redundant ecto queries in the following scenario. 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?

EDIT: the "hack" below is wrong (see my comment).
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?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions