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.
https://github.com/enewbury/quarry
https://github.com/enewbury/absinthe_quarry

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)
https://elixirmix.com/reusable-ecto-code-with-mika-kalathil-emx-164

Where Next?

Popular in Announcing Top

Azolo
Hey everyone, I just released WebSockex which is a Elixir WebSocket client. WebSockex strives to work as a OTP special process, be RFC6...
New
OvermindDL1
I created a new library (rather I pulled out a couple files from my big project), it manages an operating system PID file for the BEAM. ...
New
josevalim
EDIT: since Ecto 3.0 final version is out, this post was amended to use the final versions in the instructions below. Hi everyone, We a...
New
maltoe
Hello! Came here to announce ChromicPDF, a pet project PDF generator I’ve been working on for the past few months. Why another PDF gener...
New
benlime
LiveMotion enables high performance animations declared on the server and run on the client. As a follow up to my previous thread A libr...
New
kip
Image is an image processing library for Elixir. It is based upon the fabulous vix library that provides a libvips wrapper for Elixir. I...
622 19460 194
New
restlessronin
The repo is at GitHub - cyberchitta/openai_ex: Community maintained Elixir library for OpenAI API · GitHub. Docs are at OpenaiEx User Gu...
152 10719 134
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Darmani72
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
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
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
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
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

We're in Beta

About us Mission Statement