How to select value to be the value in the property step in gremlin

298 Views Asked by At

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?

1

There are 1 best solutions below

3
On BEST ANSWER

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();