0

I'm trying to select a value from an injected array and set it as a value in the property step. This is failing because I can't use the select step inside the property step.

This is my current failing query:

await g
        ?.inject([
            { twitterPostId: 'tay', like: true, retweet: false },
            { twitterPostId: 'fay', like: true, retweet: false },
        ])
        .unfold()
        .as('a')
        .select('twitterPostId')
        .as('t')
        .V()
        .hasId(__.select('t'))
        .fold()
        .coalesce(__.unfold(), __.addV().property(t.id, __.select('t'))
        .next();

Any thoughts on how else I could accomplish that?

Kelvin Lawrence
  • 14,674
  • 2
  • 16
  • 38
Kyano
  • 174
  • 1
  • 8

1 Answers1

1

The property() can take a traversal as a property value. I think your issue here is different.

The fold() step in Gremlin is a reducing barrier step. Any use of as() labels in a traversal are lost once you cross a reducing barrier. Instead of using as('t'), try using aggregate('t'). Then inside of the property() step, do a select('t').unfold()... such as:

await g
        ?.inject([
            { twitterPostId: 'tay', like: true, retweet: false },
            { twitterPostId: 'fay', like: true, retweet: false },
        ])
        .unfold()
        .as('a')
        .select('twitterPostId')
        .aggregate('t')
        .V()
        .hasId(__.select('t'))
        .fold()
        .coalesce(__.unfold(), __.addV().property(t.id, __.select('t').unfold())
        .next();
Taylor Riggan
  • 1,963
  • 6
  • 12
  • That worked thank you! But I got another issue now, I switch the ```V()..hasId(__.select('t'))``` step to ```V(__.select('t'))```, but I'm getting this error now: "Expected an id that is convertible to String but received class org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal\". Also tried ```V(__.select('t').unfold())``` and it returned the same error, and if it wasn't clear this failed as well ```V()..hasId(__.select('t'))``` – Kyano Jul 27 '22 at 14:25
  • So as of today, you cannot use `select()` inside of a `V()`. You'd most likely need to run two queries at this point - one to fetch the vertex IDs that you need and another to submit them as a list of vertex IDs in the following query. – Taylor Riggan Jul 29 '22 at 13:15
  • I opened a Jira for this, in case you want to +1 it. https://issues.apache.org/jira/browse/TINKERPOP-2777 – Taylor Riggan Jul 29 '22 at 17:02