Hi Nick,
In your case, you probably want to use `store` instead of `as`. `store` allows you to store traversal to memory so that you can use them later, while `as` just names a current traversal step, and reducing steps (such as fold) "forget" all the past steps and just keep the current state.
Do be careful since `store` saves the variable in a list, which is a bit confusing. I advise you to use `select` steps together with `toList` to see what the variables look like, I've found it a very useful debug trick.
In your case, this is what the code would look like:
g.V().
hasLabel('user').
has('userId', 'id1').
fold().
coalesce(unfold(), addV('user').property(Cardinality.single, 'userId', 'id1')).
store('userVertex').
V().
hasLabel('email').
has('emailId', 'id1').
fold().
coalesce(unfold(), addV('email').property(Cardinality.single, 'emailId', 'id1')).
// example adding an edge from the current vertex email to the user vertex
addE("has_email").to(select('userVertex').unfold()).
iterate()
to understand why you need the unfold for the select userVertex, try this query:
g.V().
hasLabel('user').
has('userId', 'id1').
fold().
coalesce(unfold(), addV('user').property(Cardinality.single, 'userId', 'id1')).
store('userVertex')
.fold()
.select('userVertex').unfold()
.toList()
and compare against
g.V().
hasLabel('user').
has('userId', 'id1').
fold().
coalesce(unfold(), addV('user').property(Cardinality.single, 'userId', 'id1')).
store('userVertex')
.fold()
.select('userVertex')
.toList()
PS: I'm using gremlin-python, which has some very slight syntax differences. I've tried to fix the code in "native" gremlin but there might be small mistakes