Quarry & AbsintheQuarry - data driven ecto query builder for nested loading/filtering/sorting & Absinthe integration

Hi folks,
After implementing and reimplementing various dynamic query building mechanisms for resolving graphql fields at several clients I’ve worked for, I went looking for a package that could help me out, but the only one I found required a pretty pricey buy-in, injecting macros throughout your schema files, and it didn’t have a nice integration for absinthe. Eventually I built quarry and the absinthe integration absinthe_quarry which you can use as you feel comfortable without leaking the abstraction around your codebase. It gets everything it needs from the parameters you pass. Here’s an example call to the base Quarry Module

Quarry.build!(Post, filter: %{author: %{name: "John"}}, load: [:author, comments: :user])

Just based on the top level schema module, it can build an ecto query, joining in and reusing those joins for filtering and loading.

The absinthe integration is as simple as calling the top level quarry helper function, and including meta tags on nested associations to load them

field :posts, list_of(:post) do
  arg :filter, :post_filter
  resolve quarry(Post, Repo)

object :post do
  field :title, :string
  field :author, :author, meta: [quarry: true]

Check out the readmes and docs for more detail, and I’ve got a tongue-in-cheek blog post walking though why these packages exist, and how you might use them.

The packages are still brand new, so I haven’t implemented all the overrides and escape-hatches that you’d probably want and need in production, but I have github issues in for some of them. Feel free to add thumbs up to the ones that are important to you, or add new issues.


Hi @enewbury :slight_smile: as a user of Absinthe and also having gone on similar journey of looking for query building libraries this is very interesting!
Thought I’d point you to a couple libraries/functions I currently use if they can help for possible inspiration or integration:

  • ecto_shorts by @MikaAK is a rare library that doesn’t rely on macros to accomplish dynamic query building
  • join_preload which helps to easily join & preload associations (using Query.preload rather than Repo.preload), though I’d love a non-macro way of doing the same…
  • reusable_join in the same library which de-duplicates joins, though your if Ecto.Query.has_named_binding?(query, binding) may be achieving the same goal?
1 Like

Hey thanks mayel for the tag!

There’s also a blog post to accompany ecto shorts as well as a podcast!
Learn Elixir | Creating Reusable Ecto Code (working through a deploy issue breaking styles currently, hurray for swapping to a M1 mac)
Elixir Mix: Reusable Ecto Code with Mika Kalathil - EMx 164

1 Like