Introducing Gremlex

An open source Gremlin server driver for Elixir

Kevin Moore
Oct 19, 2018 · 5 min read

Also written by Sam Havens

If you want to use a graph database in your Elixir application, but don’t want to write database commands as raw strings, you’re in luck. Gremlex is a lightweight, simple to use library for composing Gremlin queries. The Gremlin graph-traversal language is available on all Apache TinkerPop™- enabled graph databases, including AWS Neptune, CosmosDB, Neo4j, and OrientDB.

At CarLabs, we store our more complex conversation logic in AWS Neptune. We need to read from and write to Neptune from within our Elixir applications and we want to do this in a functional way that feels like idiomatic Elixir — and thus Gremlex was born. After a few months of using it in production, we are happy to present Gremlex to the rest of the Elixir community. It’s open-source, available on GitHub and Hex, and MIT-licensed.

Creating queries

Gremlex is easy to use if you know Gremlin and makes learning about Gremlin functions easy if you are a newcomer with the support of Elixir tools.

Let’s take a Gremlin query:

and turn it into a Gremlex query:

We also support nested queries. Let’s take a more complex example by writing a nested Gremlin query

which can be represented in Gremlex as

How does Gremlex work under the hood?

Every query and subquery begins with Graph.g() which creates an Erlang queue. Each function takes a queue and 0 or more arguments. That function updates the queue by appending a tuple, where the first value in the tuple is the Gremlin function name and the second is its arguments.

This allows Gremlex to easily build queries by passing them through a series of functions using the pipe operator.

Once we call Client.query we compile the query to the Gremlin equivalent and make a request using that Gremlin query.

This is cool, but can you just tell me exactly how to use it?

First off, to use this Gremlin server driver, you need a Gremlin server running. If you haven’t done that before, see the instructions over on the Apache TinkerPop Getting Started page.

To install Gremlex from

You can configure Gremlex by adding the following to your config.exs:

Gremlex uses confex, so that you can easily define your configuration to use environment variables when it comes time to deploying. To do so, simply have the parameters that need to be dynamically read at run time set to {:SYSTEM, "ENV_VAR_NAME"}.

Example Time

Our goal is to create a vertex with a property, create a second vertex, and create a relationship between the two vertices. The following functions are not delivered with Gremlex, but this will give you a basic idea of how to use Gremlex in Elixir.

Step 1 — Create an initial vertex:

The function create_vertex takes in a label and creates a vertex with the given label. The return is a tuple of an atom :ok or :error and a list of Gremlex Vertices, or for our specific use case, the vertex that we created.

Step 2 — Add a property

The function add_property takes a vertex id, a property name, and a property value. The return is a tuple of :ok and a list with our updated vertex with the new property that we added.

Step 3 — Create a vertex and create a relationship

The create_relationship function takes three parameters and pattern matches on {:ok, target}, a vertex id meant to be the source of the relationship, and a label name for the edge that will be created. In the example above, you can see that first we create a vertex and pass in the response to the create_relationship function. Since we are passing in a list of vertices as target, we are going to get the first element of the list as the target of the relationship. The return value of this function is a tuple with :ok and a list of Gremlex Edges which describes the relationship.

Hopefully these examples give you a good basis on how to utilize Gremlex in your own applications.

The Future

We at CarLabs think that Elixir and graph databases are two underutilized pieces of technology, and we believe that both will continue to grow in popularity as more people are exposed to their power and ease-of-use. If you agree, then why not try out Gremlex today? Or even better, submit a PR — it’s early days, and there are still lots of beginner-friendly issues (there are harder issues too, if that’s your style).

We’re a friendly bunch, so if you have questions, feel free to open an issue on GitHub or leave a comment here. And of course, if you’re a car manufacturer or dealer looking to transform the way you communicate with your shoppers, owners, and finance customers… we have a lot to talk about.

Finally, special thanks to Becca Lasky for the amazing logo (seriously, if you have seen both the Elixir and TinkerPop logos, you realize how perfect it is), and to GitHub/@JohannesSoots for the big PR adding nested queries 👏👏👏


Discussions about tech, automotive, and the future

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store