Coalesce object or null
jonathaneckman opened this issue · comments
Is your feature request related to a problem? Please describe.
Yes. I would like to Coalesce to return either an object or null.
The gremlin query snipper will return an object with "id" if a vertex is found on the "serves" relationship. If a vertex is not found, an empty string is returned.
.by(coalesce(out('serves')
.project('id')
.by('id'), constant('')))
Describe the solution you'd like
I would like to recreate this in Gremlinq.
The following is close to the original Gremlin query, but throws a compile error: The type arguments for method Coalesce cannot be inferred from the usage. Try specifying the type arguments explicitly.
.Coalesce(
__ => __
.Out<Serves>()
.OfType<Meal>()
.Project(e => e
.ToDynamic()
.By("id", __ => __.Id)
),
__ => __.Constant("")
)
This compiles and is what I'd ultimately like to have, but throws a runtime error: Value of variable _w is not a constant type. Cannot assign complex values to groovy variable.
.Coalesce(
__ => __
.Out<Serves>()
.OfType<Meal>()
.Project(e => e
.ToDynamic()
.By(nameof(EventMealDto.Id), __ => __.Id)
)
.Cast<EventMealDto>(),
__ => __.Constant(new EventMealDto {})
)
Describe alternatives you've considered
Still exploring Gremlin alternatives for this use case.
.By("meal", __ => if out returns a result, the result; otherwise null, an empty object, or skip this By all together)
Ignore this. Still learning Gremlin. This is much easier and more effective way to do what I wanted:
.by(out('serves')
.project('id')
.by('id').fold())