
Azure Cosmos DB is the globally distributed, multi-model database service from Microsoft for mission-critical applications. It is a multi-model database and supports document, key-value, graph, and column-family data models.
The Azure Cosmos DB Gremlin API is used to store and operate with graph data on a fully managed database service designed for any scale.
I do not want to go in deep around the concepts and idea of a GraphDB(gdb). You can get great tutorials online.
Azure Cosmos DB comes with Gremlin API for .NET that you can use to query the GDB. But in the world of easy integration needs and API enablement its very important to have the .NET Gremlin API as REST.
Basically its about executing Gremlin queries/statements in order to perform CRUD operations
Its actually easy to Restify your germlin .NET API querying. We can just write a generic HTTP triggered Azure function that takes in a gremlin query and executes it
public static class Function1
{
private static string hostname = Environment.GetEnvironmentVariable("graphdbhostname");
private static int port = 443;
private static string authKey = Environment.GetEnvironmentVariable("graphdbkey");
private static string database = Environment.GetEnvironmentVariable("graphdbname");
private static string collection = Environment.GetEnvironmentVariable("graphdbcollection");
[FunctionName("Function1")]
public static async Task<HttpResponseMessage> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string query="";
if (req.Method.ToLower() == "get")
{
query = req.Query["query"];
}
if (req.Method.ToLower() == "post")
{
query = JObject.Parse(await new StreamReader(req.Body).ReadToEndAsync())?["query"]?.ToString();
}
string output = "";
string jRes="";
var gremlinServer = new GremlinServer(hostname, port, enableSsl: true,
username: "/dbs/" + database + "/colls/" + collection,
password: authKey);
if (!String.IsNullOrEmpty(query))
{
using (var gremlinClient = new GremlinClient(gremlinServer, new GraphSON2Reader(), new GraphSON2Writer(), GremlinClient.GraphSON2MimeType))
{
// Create async task to execute the Gremlin query.
var resultSet = gremlinClient.SubmitAsync<dynamic>(query).Result;
foreach (var result in resultSet)
{
output = JsonConvert.SerializeObject(result);
}
}
jRes = String.Format("{{\"result\":{0}}}", output);
}
return String.IsNullOrEmpty(query)
? new HttpResponseMessage(HttpStatusCode.BadRequest)
{
Content = new StringContent(String.Format("{{\"result\":{0}}}", "Please pass query in the Get query or POST body"), Encoding.UTF8, "application/json")
}
: new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(jRes, Encoding.UTF8, "application/json")
};
}
}

You can save your settings in the application settings in your Azure functions

And your function is ready to execute gremlin queries and return JSON result. The function works for both GET and POST


Basically any gremlin query can be executed via this function.

Further more using API management the function can be secured and via different operations on the API, specific gremlin actions can be exposed giving a more granular expose on the API layer and yet having one function on the backend. (May be the response json formatting would need some work to fit to some gremlin query response)
3 thoughts on “RESTify your Gremlin .NET API using Azure function”