gremlintinkerpoptinkerpop3

Why is addE failing with a string reference to an earlier step?


I'm trying to create a Vertex and an Edge if the vertex doesn't already exist. This is the Gremlin query I'm trying to run:

g.V(4128).as('parent').out().has('qt', 1).fold().coalesce(unfold(), addV('test').addE('test_edge').from('parent'))

but its throwing error:

addE(test_edge) failed because the from() traversal (which should give a Vertex) failed with: The provided traverser does not map to a value

I've tried a few variations to figure out what is the problem and these both work:

g.V(4128).as('parent').out().has('qt', 1).fold().coalesce(unfold(), addV('test'))


g.V(4128).as('parent').out().has('qt', 1).fold().coalesce(unfold(), addV('test').addE('test_edge').from(__.V(4128)))

Because I have the Vertex id I can use this second variation, but I'd really like to understand where the original query is going wrong. I'm not sure why, but I think from('parent') is failing because there are no existing edges on Vertex 4128. This error didn't show up until I dropped the Graph so I could test without all my dev clutter.

g.V(4128).as('parent').out()  // empty

Solution

  • The labels that you use with an as() step do not persist after the traversal crosses a collapsing barrier step (which, fold() is a collapsing barrier).

    Since you're already referencing the parent vertex by ID, you could just do the same in the from() step without the need for an as-label:

    g.V(4128).
        out().has('qt', 1).
        fold().coalesce(
            unfold(), 
            addV('test').addE('test_edge').from(V(4128))
        )