arjan

arjan

Would there be interest in a (small) DSL for writing graphql queries in Elixir?

In my tests I am now annoyed by the fact that GraphQL does not indent very well (writing gigantic GraphQL queries inside ~s(...)), so I’m tempted to write a macro which takes something like this:

    graphql = query [id: :string] do
      orders(a: id) do
        id
        title
        order_lines do
          id
          price
        end
      end
    end

And automagically converts it into this string:

query ($id: String) { orders(id: $id) { id title order_lines { id price } } }

@benwilson512 @bruce

Showing Posts 17 to 8

arjan

arjan OP

Yes that would be awesome. I see that Sangria has a graphql macro as well to check the query compile-time.

OvermindDL1

OvermindDL1

Mine is more about calling into the locally defined absinthe system.

keathley

keathley

This is the main benefit of github’s graphql-client: https://github.com/github/graphql-client. It validates your queries when you define them which is about as close as you can get to a “compile time” check in ruby.

If a DSL could be incorporated into absinthe then that seems great. Ideally you could use any schema. My use case is less about testing and more about interacting with external services though.

law

law

I’d stayed away from generating queries dynamically primarily from this advice about the benefits of static graphql queries.

But this has me re-thinking. Tooling support and compile time checks are really appealing. This code is really nice to read.

Not sure what conclusion I’ll come to, but awesome work.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Yeah your DSL just needs to build a blueprint tree. See the Draft protocol in Absinthe.

OvermindDL1

OvermindDL1

Ooo, that is awesome, being able to run that will be fantastic for verifying good strings at compile-time and no doubt for a DSEL too. :slight_smile:

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

lol I hit enter too early. Fixed now with a real reply.

OvermindDL1

OvermindDL1

Meant a valid query, not schema. ^.^;

arjan

arjan OP

I’ve put it up: GitHub - arjan/absinthe_dsl: Elixir DSL for constructing GraphQL queries · GitHub
Works like this:

    query = query(arg: :string) do
      users(id: arg) do
        name
      end
    end

    assert query == "query ($arg: String) { users(id: $arg) { name } }"

Contributors welcome :slight_smile:

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Absinthe works a lot like a compiler, so you just build a pipeline of phases up prior to the actual resolution point:

validation_pipeline = schema
|> Absinthe.Pipeline.for_document(jump_phases: false)
|> Absinthe.Pipeline.before(Absinthe.Phase.Document.Execution.Resolution)

error_result_pipeline = [Absinthe.Phase.Document.Result]

case Absinthe.Pipeline.run(document, pipeline) do
  {:ok, blueprint, _} -> :ok
  {:error, blueprint, _} ->
    {:ok, %{result: result}, _} = Absinthe.Pipeline.run(blueprint, error_result_pipeline)
    {:error, result}
end

It isn’t quite as easy as I’d like. I’m gonna add a phase I think that will automatically run the result phase ONLY on error. Either way, this should get you going for now.

Where Next? Top

Trending in Discussions Top

cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
axelson
Hi there! :wave: @frigidcode and I (but mostly him) have been running an Elixir Book club, we’re almost done with Designing Elixir Syste...
New
achempion
I’ve been using Emacs as my main code editor for more than a two years. It’s a custom build version although I’ve tried doom emacs and sp...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New
jtormey
Lately I’ve been thinking about how to organize components as a LiveView application grows. One of the pain points I’ve found (for myself...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
georgeguimaraes
Just published claude-code-elixir, a plugin marketplace for Claude Code with Elixir support. These are the plugins I’ve been using for my...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews