I've been trying for several hours to write a gremlin statement to handle upserting 2 vertices and 1 edge between them, without much luck.
in pseudo-gremlin what I want to do is pretty straightforward and is the following:
g.V()
.hasLabel("person")
.has("custom_id", "123")
.fold()
.coalesce(
__.unfold().property(single, "name", "Tom"),
__.addV("person").property(single, "custom_id", "123").property(single, "name", "Tom"))
.as("fromStep")
.V()
.hasLabel("person")
.has("custom_id", "654")
.fold()
.coalesce(
__.unfold().property(single, "name", "Sally"),
__.addV("person").property(single, "custom_id", "654").property(single, "name", "Sally"))
.as("toStep")
.E()
.hasLabel("knows")
.where(__.inV().is("fromStep"))
.where(__.outV().is("toStep"))
.fold()
.coalesce(
__.unfold().property("since", "2020-01-01"),
__.addE("knows").property("since", "2020-01-01").from("fromStep").to("toStep")
The problem with this code is that each fold step, being a barrier step "removes" the value of the previous as steps.
Is there a way to do this properly in a single statement?
SOLUTION
Thanks to Kelvin answer's here's the solution :)
g.V()
.hasLabel("person")
.has("custom_id", "123")
.fold()
.coalesce(
__.unfold().property(single, "name", "Tom"),
__.addV("person").property(single, "custom_id", "123").property(single, "name", "Tom"))
.store("a")
.V()
.hasLabel("person")
.has("custom_id", "654")
.fold()
.coalesce(
__.unfold().property(single, "name", "Sally"),
__.addV("person").property(single, "custom_id", "654").property(single, "name", "Sally"))
.store("b")
.fold()
.select("a").unfold().as("from")
.select("b").unfold().coalesce(
__.inE("knows").where(__.outV().as("from")).property("since", "2020-01-01"),
__.addE("knows").property("since", "2020-01-01").from("from")
)