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 } } }
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.
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. ^.^
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.
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?).
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.
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.