enewbury

enewbury

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)
end

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

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.

Most Liked

mayel

mayel

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?
MikaAK

MikaAK

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

Where Next?

Popular in Libraries Top

Crowdhailer
Raxx is an alternative to Plug and is inspired by projects such as Rack(Ruby) and Ring(Clojure). 1.0-rc.1 is now available. To use it re...
New
woylie
I released Doggo, a collection of unstyled Phoenix components. Features Unstyled Phoenix components. Storybook that can be added to...
New
mbuhot
Leverage Open Api 3.0 (Swagger) to document, test, validate and explore your Plug and Phoenix APIs. Generate and serve a JSON Open API ...
New
arkgil
Hi all! I’m happy to announce that Telemetry v0.3.0 is out! This release marks the conversion from Elixir to Erlang so that all the libr...
New
Qqwy
Hello everyone, I wrote a small library today called MapDiff. It returns a map listing the (smallest amount of) changes to get from map...
New
mbuhot
EctoJob A transactional job queue built with Ecto, PostgreSQL and GenStage Available on Hex.pm: ecto_job | Hex Docs: API Reference — ec...
New
wmnnd
Hi there, for my project DBLSQD, I needed a file storage solution that is a bit more flexible than Arc. Because I thought others might f...
New
mischov
import Meeseeks.CSS html = HTTPoison.get!("https://news.ycombinator.com/").body for story <- Meeseeks.all(html, css("tr.athing")) do...
New
wojtekmach
Hey everyone! Req is an HTTP client for Elixir that I’ve been working on for quite some time. There is already a lot of HTTP clients out...
New
New

Other popular topics Top

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
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
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
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
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
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 42633 214
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Sub Categories:

We're in Beta

About us Mission Statement