-1

Why .as().step used before .fold() is not able to use/refer after .fold() in apache Gremlin?

Eg:

g.V().hasLabel('country').has('name', 'Japan').fold()
        .coalesce(__.unfold(), __.addV('country').property('name', 'Japan')).as('country')
    .outE('has').inV().hasLabel('state').has('name', 'A').fold()
        .coalesce(__.unfold(), __.addV('state').property('name', 'A').addE('has').from('country'))

What is the alternative step to as?

Thirumal
  • 8,280
  • 11
  • 53
  • 103
  • Your question is bit misleading. As fold step followed by as is not the problem here. You could have run partial query without addE step to gaze into problem and frame question better. – PrashantUpadhyay Nov 30 '22 at 06:59

1 Answers1

2

Your query while traversing gets into a problem of filtering all the available values. So when the add Edge is called it doesn't have any value bind to the label country

You can change your query a bit to make sure the value for label is country is not filtered. I wrote a simple rewrite below which does that for you.

gremlin> g.V().
......1>   hasLabel('country').
......2>   has('name', 'Japan').
......3>   fold().
......4>   coalesce(__.unfold(), __.addV('country').property('name', 'Japan')).
......5>     as('country').
......6>   coalesce(
......7>     outE('has').inV().hasLabel('state').has('name', 'A'),
......8>     __.addV('state').property('name', 'A')).
......9>   addE('has').from('country')
==>e[18][14-has->16]
gremlin> g.V().valueMap()
==>[name:[A]]
==>[name:[Japan]]
gremlin> g.E()
==>e[18][14-has->16]

UPDATED 2023-03-17

In TinkerPop release 3.6.0 two new steps mergeV and mergeE were added to Gremlin. These are designed to make the "create if not exist" or "upsert" type queries much easier to write. In most cases these new steps will replace the former fold...coalesce pattern. As of version 1.2.1.0 Amazon Neptune now supports the required TinkerPop version. For further reading please checkout the TinkerPop documentation

Kelvin Lawrence
  • 14,674
  • 2
  • 16
  • 38