pwightman

pwightman

How to access schema information during Ecto query generation when using Absinthe/Dataloader?

Hi, I’m using Absinthe with Dataloader and have a question.

Here’s some code that mimics the example from the tutorial very closely (found here), all the usual suspects:

defmodule MyApp.Schema do
  # ...
  object :post do
    field :title, :string
  end

  object :author do
    field :posts, list_of(:post) do
      arg(:limit, :integer)
      resolve(dataloader(MyApp.Blog))
    end
  end

  def context(ctx) do
    loader =
      Dataloader.new()
      |> Dataloader.add_source(MyApp.Blog, MyApp.Blog.data())

    Map.put(ctx, :loader, loader)
  end

  def plugins do
    [Absinthe.Middleware.Dataloader] ++ Absinthe.Plugin.defaults()
  end
end

And then MyApp.Blog:

defmodule MyApp.Blog do
  # ...
  def data() do
    Dataloader.Ecto.new(MyApp.Repo, query: &query/2)
  end

  def query(queryable, params) do
    # How can I get params[:limit] to be the :limit arg from the schema???
    queryable
    |> limit(params[:limit] || 20)
  end
end

My question is: how can I access the limit arg attached to the posts field on author from the Schema, such that I can use an Ecto limit query inside MyApp.Blog.query/2? I can see in the docs that you can pass params along to the query function by passing them along in the Dataloader.load call, but from what I can tell, that is called internally.

Is there a way to pass the limit arg through that doesn’t involve abandoning dataloader and falling back to the raw batch function?

Thanks for any help!

Marked As Solved

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Hey @pwightman. To start with, the load function is not private or internal, it’s the main API for Dataloader itself. The dataloader/1 helper is great, and covers a lot of main use cases, but in certain complex scenarios using load/4 directly is perfectly fine.

The dataloader/1 helper actually passes along all arguments the resolver has to the query function, so you should actually see limit in there already, have you looked for it?

Unfortunately though, using a limit here is probably not what you want. limit when applied to an SQL query applies to the total number of result rows, whereas the meaning or authors { posts(limit: 20) } would normally be “20 posts per author”.

To limit the way you want requires window functions, which are not yet supported out of the box by dataloader. You can find some discussion of this here: Connection.from_dataloader/5 helper? · Issue #128 · absinthe-graphql/absinthe_relay · GitHub This is specifically about relay connection queries but the principle is the same.

Also Liked

pwightman

pwightman

Ah, very true. Mostly I was using this as an example of passing arbitrary values to fields, accessing the schema generally, etc, but I had forgotten about this issue with limit specifically.

I feel a little like I’m going crazy, but yes, it turns out query/2 does receive all args already, I checked this specifically last night before posting and could have sworn it wasn’t, but upon checking again this morning, there they are.

Digging a bit, I was also happy to find there’s a dataloader/3 with all sorts of useful information in it as well, so should be able to access everything I need.

Thanks for all your work on Absinthe, really enjoying it!

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
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
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
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
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43622 214
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47930 226
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New

We're in Beta

About us Mission Statement