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 1 to 10

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

This is a good question. TBH I’m not sure it gets you much over using triple quotes:

query = """
query ($id: String) {
  orders(id: $id) { id title orderLines { id price } }
}
"""

GraphQL syntax is so very nearly a DSL on its own already. Certainly for tests I’m not sure that a DSL adds much value.

arjan

arjan OP

You’re right.. the """ strings solve most issues. Except that it’s not smart with auto-indentation in the string. But I guess that’s an Emacs issue :wink:

keathley

keathley

We just use triple quoted strings for queries and its working out pretty well. I’m not sure what the main benefits a DSL would offer over just using strings. There could be some benefit to an elixir, graphql client that provided introspection, type hints, and some sort of query validation though.

OvermindDL1

OvermindDL1

Heck yes! I’ve been wanting one for a while! I’ve just been writing strings so far, not got around to trying to make a DSEL yet. ^.^

Doing it in Elixir might be a bit more verbose, but if the DSEL is designed well then it could catch a lot of bugs at compile-time, especially if it introspects an Absinthe schema to verify against. ^.^

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

I hear you, but I actually think a sigil would be the best thing for that IE

query = ~A"""
query ($id: String) {
  orders(id: $id) { id title orderLines { id price } }
}
"""

Having actual graphql docs in tests has provided me with tons of utility because I can just directly link to test cases when other developers ask for examples. Obviously GraphiQL is a good exploration tool too, but it’s been pretty handy to have a full set of tested docs ready for usage.

arjan

arjan OP

Yes, it would indeed validate the given schema.

I’ve made some progress on this, I’ll extract it from my current code base and put it in a repo.

OvermindDL1

OvermindDL1

Actually, I wonder about combining the ideas.

What about a text definition (via “”") but it calls absinthe at compile-time to validate it is a valid ~schema~/call (does absinthe have a way to validate a call without actually calling it?).

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.

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:

OvermindDL1

OvermindDL1

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

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 92995 915
New
caslu
I want to open this thread for you all to discuss and help those who really like Ash but are still hesitant to use it in a real project. ...
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
GES233
I’m posting this in response to Jose’s recent tweet (Cr. link) : People are sleeping on Elixir for a coding harness: Hot-code swappi...
New
_mfierro
Hello, I wrote Stop My Hand, a Scattergories-like web application using Phoenix/LiveView as my learning project for Elixir (after readin...
New
nseaSeb
AcmeScript — Writing JS hooks as if I were still using Elixir I’ve been having fun building a little something over the last few days: Ac...
New
marciol
It would be helpful to have a list of companies worldwide that hire engineers without prior experience in Elixir. Often, it can be quite ...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
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
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews