Search code examples
node.jsgremlintinkerpopamazon-neptune

AWS Neptune, JS, Gremlin: add Vertex with optional Edge


Sometimes there is need to create Vertex with optional Edge.

g.addV('label')
.property(id, 'uniq_id_2').as('u')
.property('edge_is_needed', edgeIsNeeded)
.constant(edgeIsNeeded)
.choose(eq(true), 
  addE('connected').from('u').to(V('uniq_id_1'))
)
.select('u')
.toList()

This Traversal works, I just injecting boolean value with edgeIsNeeded variable in JS.

Is there a better way to do it in single Traversal, for example, based on previous property edge_is_needed value?


Solution

  • You don't need any path information / step labels for this query and no choose() complexity. It's just a side-effect with a simple has() filter:

    g.addV('label').
        property(id, 'uniq_id_2').
        property('edge_is_needed', edgeIsNeeded).
      sideEffect(has('edge_is_needed', true).
                 addE('connected').to(V('uniq_id_1')))