I wasn’t sure if I should have instead made this as an issue on the JS driver GitHub, but this is a fairly “general” thing anyways - I suspect this applies to multiple languages.
q.Foo() functions. However, with the introduction of template literals a while back into JS, its possible to use a tagged template literal to create FQL query strings.
Using a tag function, any FQL template literal strings could be evaluated and transformed into an instance of the already existing
FaunaDB.Expr class. Done like this, there shouldn’t be any compatibility issues, even when used with query functions. I suspect there would be a lot of implementation details to be hashed out, though.
Some of the implications of using template literals are pretty great. For me, it’s primarily three things:
- Template literal queries would be more portable in general, with theoretically complete compatibility with anything written in Fauna Shell.
- I think queries are objectively more readable and easier to parse when written in pure FQL.
- Very slick editor support for FQL queries, as a template literal tagged with
FQLcould be parsed by an editor as an FQL language string, complete with syntax highlighting and autocomplete, if provided.
I figured (at first) that it wouldn’t be difficult to just create the function myself, so I did a bit of snooping around. I wanted to make a implementation that would look like this:
Client.query( fql`Select("fun", [you_get_it])` )
However, as I looked around the JS driver source I realized the JS functions were not composing an FQL query string as I had originally suspected. So, I instead took a look at the Fauna Shell parser code, and saw that it had to do a lot of work to parse input FQL and create a query.
TLDR, I realized it wasn’t worth doing this myself. Even if I did, I’d have to bundle large portions of the parser code and I didn’t feel like doing that.
So consider this my humble request for this feature. I’m sure a concern is compatibility - but I think feature detection could allow this to exist as an optional supplement to the current query functions. Front-end developers already know to not use template literals if they’re targeting older browsers.