Search code examples
gremlingraph-databasesupserttinkerpopamazon-neptune

Gremlin - Vertex and Edge Upsert in 1 traversal


I have a Vertex upsert working and an Edge upsert working in 2 separate traversals. Is it possible to combine the 2 into 1 traversal? I have attempted but am receiving errors.

a = self.g.V().has('account', 'id', 'account-111111111').fold().coalesce(.unfold(),.addV('account').property(T.id, 'account-111111111')).has('customer', 'id', 'cust-111111111').as_('v').V().has('account', 'id', 'account-111111111').coalesce(.inE('owns').where(.outV().as_('v')),.addE('owns').from_(.V('customer', 'id', 'cust-111111111')))

a.next()

Works:

Vertex Upsert:

a = g.V().has('account', 'id', 'account-111111111').fold().coalesce(.unfold(),.addV('account').property(T.id, 'account-111111111')) a.next()

Edge Upsert:

a = g.V().has('customer', 'id', 'cust-111111111').as_('v').V().has('account', 'id', 'account-111111111). \ coalesce(.inE('owns').where(.outV().as_('v')),.addE('owns').from_(.V('customer', 'id', 'cust-111111111])))

a.next()


Solution

  • After studying it for a while, there are quite a few things in your example that will not work.

    First of all if you know the ID of a vertex or an edge there is really no need to check the label or any other property.

    Secondly T.id is an ID. The value 'id' would be a property called 'id'.

    From your question it was a bit hard to piece together what you are wanting to do but here is a first go at what I think you intended. If this is not in fact what you wanted please edit the question to make it more clear. Anyway, I hope this helps

    g.V('account-111111111').
      fold().
      coalesce(__.unfold(),
               __.addV('account').property(T.id, 'account-111111111')).
      coalesce(__.in('owns').hasId('cust-111111111')),
               __.addE('owns').from_(__.V('cust-111111111'))