gelx
Generate fully typed rust code from your gel schema and inline queries with
gelx.
Installation
To install the gelx crate you can use the following command.
Or directly add the following to your Cargo.toml file.
= "0.3"
Follow the Quickstart Guide to make sure your gel instance is running. The macro relies on the running gel instance to parse the output of the provided query string.
Usage
When working with gel you often need to write queries and also provide the types for both the input and output. Your code is only checked at runtime which increases the risk of bugs and errors.
Fortunately, gel has a query language that is typed and can be converted into types and queried for correctness at compile time.
Inline Queries
use Error;
use create_client;
use gelx;
// Creates a module called `simple` with a function called `query` and structs
// for the `Input` and `Output`.
gelx!;
async
The macro above generates the following code:
Query Files
Define a query file in the queries directory of your crate called select_user.edgeql.
# queries/select_user.edgeql
select User {
name,
bio,
slug,
} filter .slug = <str>$slug;
Then use the gelx macro to import the query.
use Error;
use create_client;
use gelx;
// Creates a module called `select_user` with public functions `transaction` and
// `query` as well as structs for the `Input` and `Output`.
gelx!;
async
Configuration
The following configuration options are supported.
[]
## The location of the queries relative to the root of the crate.
= "./queries"
## The features to enable and their aliases. By default all features are enabled.
## To disable a feature set it to false. The available features are:
## - query
## - serde
## - strum
## - builder
= { = "ssr", = "ssr", = "ssr" }
## The location of the generated code when using the `gelx` cli.
= "./src/gelx_generated.rs"
## The name of the arguments input struct. Will be transformed to PascalCase.
= "Input"
## The name of the exported output struct for generated queries. Will be transformed to PascalCase.
= "Output"
## The name of the query function exported.
= "query"
## The name of the transaction function exported.
= "transaction"
## The relative path to the `gel` config file. This is optional and if not provided the `gel`
## config will be read from the environment variables.
# gel_config_path = "./gel.toml"
## The name of the `gel` instance to use. This is optional and if not provided the environment
## variable `$GEL_INSTANCE` will be used.
# gel_instance = "$GEL_INSTANCE"
## The name of the `gel` branch to use. This is optional and if not provided the environment
## variable `$GEL_BRANCH` will be used.
# gel_branch = "$GEL_BRANCH"
CLI
The gelx_cli crate exposes a binary called gelx which can be used to generate the typed code into rust files rather than inline queries.
It should be run from the crate directory and will read from the configuration specified in the previous section.
Sometimes you will need to check that the generated code matches the current database schema and queries generated by gel. This is useful for CI pipelines.
If there are changes that haven't been accounted for, the check will fail and you should regenerate the code.
Future Work
This crate is still in early development and there are several features that are not yet implemented.
Missing Types
Currently the following types are not supported:
MultiRange- The macro will panic if a multirange is used.
MultiRange
These are not currently exported by the gel-protocol so should be added in a PR to the gel-protocol crate, if they are still supported in the new protocol.
LSP parsing
Currently the macro depends on having a running gel instance to parse the query string.
Once an LSP is created for gel it would make sense to switch from using string to using inline gel queries.
use gelx;
gelx!;
Features
default— The default feature iswith_all.with_bigint— Include thenum-bigintdependency.with_bigdecimal— Use thebigdecimalcrate.with_chrono— Use thechronocrate for all dates.with_all(enabled by default) — Include all additional types. This is included by default. Usedefault-features = falseto disable.builder— Use thetyped-buildercrate to generate the builders for the generatedInputstructs.query— Turn on thequeryandtransactionmethods and anything that relies ongel-tokio. The reason to separate this feature is to enable usage of this macro in browser environments wheregel-tokiois not feasible.serde— Enable serde for the generated code.strum- Use thestrumcrate for deriving strings from the created enums.