/CosmosDB.Gremlin.Fluent

A fluent query builder for Gremlin queries

Primary LanguageC#Apache License 2.0Apache-2.0

Build Status

Azure DevOps builds

Azure DevOps tests

CosmosDB.Gremlin.Fluent

A fluent query builder for Gremlin queries.

Azure CosmosDB Gremlin API does not support Java bytecode as of August 2019. This makes writing queries in Gremlin require constructing them using regular strings. This process is prone to errors with lack of any completions of syntax check.

To solve this issue, this library allows constructing Gremlin API queries using fluent API designed to cover all Gremlin features supported by CosmosDB Gremlin implementation. To further assist with writing production ready queries, there is support for Gremlin arguments built in. Arguments are tracked, validated and maintained automatically, allowing the developer to focus on writing queries and not keeping track of all the arguments used.

To get started, create a new instance of GremlinQueryBuilder:

var builder = new GremlinQueryBuilder()

You can then start chaining Gremlin invocations on the builder:

builder.G().V().HasId(new GremlinProperty("some identifier")).ValueMap(true);

or

var identifier = "some identifier";
builder.G().V().HasId(new GremlinArgument(nameof(identifier),identifier)).ValueMap(true);

The query can then be executed by Gremlin.NET client:

gremlinDotNetClient.SubmitAsync<Dictionary<string,object>>(builder.Query, builder.Arguments);

For queries that utilise partition keys, these can be defined directly on graph traversal source invocation

builder.G("partitonKeyName", "KeyValue1", "KeyValue2").V("some identifier");

To simplify writing queries with simple parameters, implicit conversion operators have been defined for common types, allowing to write

builder.G().V("identifier");

instead of

builder.G().V(new GremlinParameter("identifier"));

The library has support for all functions listed at the official CosmosDB Gremlin support page as well as a number of ones listed on various other Gremlin-related MSDN articles (such as support for partition key strategy definition, within and without).

Because this library does not generate any Gremlin bytecode and is merely providing an abstraction to facilitate writing string-based Gremlin queries, it can be used with a number of Gremlin client implementations. However, syntax generated is CosmosDB specific, since there are a few disagreements between TinkerPop and CosmosDB syntax flavours (one example is defining partition key strategy).