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
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.
0
Most Liked
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
6
Popular in Questions
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
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
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
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
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
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
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
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
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I’m writing a test for one of the...
New
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
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
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
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









