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

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30877 112
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
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
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New

We're in Beta

About us Mission Statement