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