Billzabob

Billzabob

Using Absinthe Dataloader for top level queries

I’m trying to figure out how to use Dataloader with absinthe for a top level query. I have it working just fine for a field on an object, like so:

defmodule UserSchema do
  use Absinthe.Schema.Notation
  import Absinthe.Resolution.Helpers, only: [dataloader: 1]
  alias Core.Loaders.CardLoader

  object :user do
    field(:cards, list_of(:card), resolve: dataloader(CardLoader))
  end
end

However, I’d like to also be able to use Dataloader for top level queries like so:

query do
  field(:cards, list_of(:card), resolve: dataloader(CardLoader))
end

But this fails with this error:

** (Dataloader.GetError)   The given atom - :cards - is not a module.

  This can happen if you intend to pass an Ecto struct in your call to
  `dataloader/4` but pass something other than a struct.

How can I achieve this? I could obviously do it another way, by having the resolver function call Repo.all(Card) but I’d love to be able to do it all with Dataloader. Is that possible?

First Post!

VictorGaiva

VictorGaiva

Usually you need explicit resolvers since most the time a query will receive parameters, from args or from the current logged user, so that you can specify how to fetch the data you want.

Most Liked

Billzabob

Billzabob

I ended up getting this working with the following:

import Absinthe.Resolution.Helpers, only: [on_load: 2]

query do
  field :users, list_of(:user) do
    arg(:ids, non_null(list_of(:id)))
    resolve(fn %{ids: ids}, %{context: %{loader: loader}} ->
      loader
      |> Dataloader.load_many(UserLoader, User, ids)
      |> on_load(fn loader ->
        users = Dataloader.get_many(loader, UserLoader, User, ids)
        {:ok, users}
      end)
    end)
  end
end

Where Next?

Popular in Questions Top

ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

We're in Beta

About us Mission Statement