I'm trying to create an upsert traversal in Gremlin. Update an edge if it exists, otherwise add a new edge.
g.V("123")
.as("user")
.V("456")
.as("post")
.inE("like")
.fold()
.coalesce(
__.unfold()
.property("likeCount", 1),
__.addE("like")
.from("user")
.to("post")
)
This returns an error.
The provided traverser does not map to a value: []->[SelectOneStep(last,post)]
I've narrowed this down to the to("post")
step. From within coalesce
it can't see post
from as("post")
. It is also unable to see user
.
This is strange to me because the following does work:
g.V("123")
.as("user")
.V("456")
.as("post")
.choose(
__.inE("like"),
__.inE("like")
.property("likeCount", 1),
__.addE("like")
.from("user")
.to("post")
)
From within the choose()
step I do have access to user
and post
.
I'd like to use the more efficient upsert pattern but can't get past this issue. I could just look up the user
and post
from within coalesce
like so:
g.V("123")
.as("user")
.V("456")
.as("post")
.inE("like")
.fold()
.coalesce(
__.unfold()
.property("likeCount", 1),
__.V("456")
.as("post")
.V("123")
.addE("like")
.to("post")
)
But repeating that traversal seems inefficient. I need post
and user
in the outer traversal for other reasons.
Why can't I access user
and post
from within a coalesce
in my first example?