Given your most recent comment on Kfir's answer that includes your latest code, I think that there are several problems to correct with your approach. First note that addV()
does not take a long list of labels and properties. I'm surprised that didn't generate an error for you. addV()
just takes the vertex label as an argument and then you use property()
to provide the associated key/value pairs.
g.V().has('User', 'refId', '435').
fold().
coalesce(unfold(),
addV('User').
property('name', 'John Smith').property('refId', '435').property('firstName', 'John').
property('lastName', 'Smith').property('JobTitle', 'Chief Executive Officer')).as('user').
V().has('JobTitle', 'name', 'Advisor').
fold().
coalesce(unfold(), addV(label, 'JobTitle', 'name', 'Advisor')).as('jobtitle').
V().
addE('REGISTERED_AS').
from('user').
to('jobtitle')
There is an extra V()
right before addE()
which would basically call addE()
for every vertex you have in your graph rather than just for the one vertex to which you want to add an edge.
g.V().has('User', 'refId', '435').
fold().
coalesce(unfold(),
addV('User').
property('name', 'John Smith').property('refId', '435').property('firstName', 'John').
property('lastName', 'Smith').property('JobTitle', 'Chief Executive Officer')).as('user').
V().has('JobTitle', 'name', 'Advisor').
fold().
coalesce(unfold(), addV(label, 'JobTitle', 'name', 'Advisor')).as('jobtitle').
addE('REGISTERED_AS').
from('user').
to('jobtitle')
So, now things look syntax correct but there is a problem and it stems from this:
gremlin> g.V(1).as('x').fold().unfold().addE('self').from('x').to('x')
The provided traverser does not map to a value: v[1]->[SelectOneStep(last,x)]
Type ':help' or ':h' for help.
Display stack trace? [yN]
The path information in the traversal is lost after the a reducing step (i.e. fold()
) so you can't select back to "x" after that. You need to reform your traversal a bit to not require the fold()
:
g.V().has('User', 'refId', '435').
fold().
coalesce(unfold(),
addV('User').
property('name', 'John Smith').property('refId', '435').property('firstName', 'John').
property('lastName', 'Smith').property('JobTitle', 'Chief Executive Officer')).as('user').
coalesce(V().has('JobTitle', 'name', 'Advisor'),
addV('JobTitle').property('name', 'Advisor')).as('jobtitle').
addE('REGISTERED_AS').
from('user').
to('jobtitle')
That really just means using coalesce()
more directly without the fold()
and unfold()
pattern. You really only need that pattern at the start of a traversal to ensure that a traverser stays alive in the stream (i.e. if the user doesn't exist fold()
produces an empty list which becomes the new traverser and the traversal will continue executing).