Search code examples
c#.net.net-coregremlinamazon-neptune

Does Gremlin.NET support submitting async bytecode?


There are extension functions for submitting queries asynchronously in Gremlin.Net, some get string which is not recommended and others use RequestMessage which is not as readable as using GraphTraversal functions.

Is there a way to submit a query like the one below asynchronously, without submitting a string or RequestMessage?

var res = _graphTraversalSource.V().Has("name", "Armin").Out().Values<string>("name").ToList();

A little more context

I'm writing an API that queries AWS Neptune. Here's how I get the GraphTraversalSource in the constructor of a singleton service (not sure if I should make the RemoteConnection a singleton and generate a GraphTraversalSource for each query or this is the right approach):

private readonly GraphTraversalSource _graphTraversalSource;

public NeptuneHandler(string endpoint, int port)
{
    var gremlinClient = new GremlinClient(new GremlinServer(endpoint, port));
    var remoteConnection = new DriverRemoteConnection(gremlinClient);
    _graphTraversalSource = AnonymousTraversalSource.Traversal().WithRemote(remoteConnection);
}

Solution

  • You can execute a traversal asynchronously with the Promise() terminator step

    var names = await g.V().Has("name", "Armin").Out().Values<string>("name").Promise(t => t.ToList());
    

    Promise() takes a callback as its argument that calls the usual terminator step you want to be executed for your traversal which is ToList in your case. If you however only want to get a single result back, then you can just replace ToList() with Next().

    Note that I renamed the variable for the graph traversal source to g as that is the usual naming convention for Gremlin.

    As I already mentioned in my comment, it is recommended to reuse this graph traversal source g across your application as it can contain configuration that applies to all traversals you want to execute. It also contains the DriverRemoteConnection which uses a connection pool for the communication with the server. By reusing g, you also use the same connection pool for all traversals in your application.