Reputation: 313
I'm trying to access the previously saved traversal using .as() in the second traversal inside coalesce() as below:
query to upsert edge (update edge if present / create)
Java code:
g.V('x').as('start')
.V('y').as('stop')
.inE('label').where(outV().as('edge'))
.select('start','stop','edge').fold()
.coalesce(unfold(),
addE('label').from(select('start')).to(select('stop')))
.property('key','value')
.promise(Traversal::Next);
Throws error as below: (precised for brevity)
gremlin.driver.exception.ResponseException: The provided traverser does not map to a value [stop]
when i replace the last step as below its working fine (instead of alias querying the vertices again)
Replaced addE('label').from(select('start')).to(select('stop'))
with addE('label').from(V('x')).to(V('y'))
Is there anyway to refer the alias in the second traversal in coalesce?
Note: I'm collecting all data related to finding edges before coalesce in order to make the gremlin throw error when any of the vertex / vertices are missing while creating edge
Expected behaviour: True on successful transaction and error when any vertex missing while creating edge.
This works as expected without using as() alias. But, i'm trying with as(). which i couldn't make it.
Hope this is clear. Please comment if in need of more info. Thanks.
Upvotes: 0
Views: 1279
Reputation: 3006
The reason you cannot select the labels 'start' and 'stop' is that you used fold()
after defining them. fold()
is a reducing barrier step that causes all the labels defined before it to be lost.
Before I explain the solution, here is the traversal to add the two test vertices.
g.addV().property(id, 'x').
addV().property(id, 'y')
The following traversal returns the string 'error' if any of the vertices 'x' or 'y' is missing. If both vertices exist, it upserts the edge (updates the edge if present or adds it if not present).
g.inject(1).
optional(V('x').as('start')).
choose(
select('start'),
optional(V('y').as('stop')).
choose(
select('stop'),
coalesce(
select('start').outE('label').as('e').inV().where(eq('stop')).select('e'),
addE('label').from('start').to('stop')).
property('key', 'value'),
constant('error')),
constant('error'))
Upvotes: 3