0

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?

aring
  • 3,422
  • 2
  • 22
  • 29

1 Answers1

2

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')))
Daniel Kuppitz
  • 10,846
  • 1
  • 25
  • 34