Search code examples
c#graph-databasesgremlindatastax-enterprise-graph

Adding a new vertex and an edge to an existing vertex in Datastax CassandraCSharpDriver.Graph but get edge OUT error


I'm adding a new vertex and an edge to an existing vertex in Datastax Graph, and I wanted to see how to do that with Datastax CassandraCSharpDriver.Graph.

The working Gremlin code looks like this:

Vertex link1 = graph.addVertex(label, "link").property("id", "link-2")
Vertex item1 = g.V().has("item", "id", "item-1").next()
item1.addEdge('contains', link1)

But in the C# driver syntax, I was hoping to do something like this but when I execute it, the error is the "adjacency of 'contains' in direction 'OUT' has not been added to 'link'"

GraphTraversalSource g = DseGraph.Traversal(mySession);
var traversal = g.AddV("link").Property("id", "link-1")
                .AddE("contains")
                .V("item").Has("id", Eq("item-1"));
GraphResultSet result = mySession.ExecuteGraph(traversal);

I had created the edge and edge connections like this:

schema.edgeLabel("contains").multiple().create()  
schema.edgeLabel("contains")  
.connection("item", "link")  
.connection("link", "item")  
.add()  

Any ideas if the schema edge is setup incorrectly or how to do this the best way in Datastax CassandraCSharpDriver.Graph?


Solution

  • Your Gremlin here:

    g.AddV("link").Property("id", "link-1")
                .AddE("contains")
                .V("item").Has("id", Eq("item-1")
    

    isn't properly formed. It should be:

    g.AddV("link").Property("id", "link-1").As('l1').
      V("item").Has("id", Eq("item-1")).
      AddE('contains').To('l1')
    

    With AddE() you need to specify the From() and the To() to identify the vertices that the edge is connecting. Without specifying those, AddE() will just use the incoming Vertex for both values creating a self-referencing edge. As a result in this case, you should only need to specify the To() as the From() is inferred.

    Please note the examples in the Reference Documentation where you should see other methods to doing this.