optional

Returns an optional result if the specified traversal yields a result; otherwise, returns the calling element.

Synopsis

optional(traversal_fragment)
Table 1. Legend
Syntax conventions Description
Lowercase and uppercase Literal keyword. Includes ().
Italics Variable value. Replace with a user-defined value.
[] Optional. Square brackets ( [] ) surround optional command arguments. Do not type the square brackets.
{} Group. Braces ( {} ) identify a group to choose from. Do not type the braces.
| Or. A vertical bar ( | ) separates alternative elements. Type any one of the elements. Do not type the vertical bar.
... Repeatable. An ellipsis ( ... ) indicates that you can repeat the syntax element as often as required.

Description

The optional() step is a branch/flatMap step that will either return the result of the specified traversal if it yields a result, or the return of the calling element.

Examples

Returns the friends of John Doe, since he has friends:
g.V().has('person', 'name', 'John Doe').optional(out('knows'))
Returns Julia Child, since she has no linked friends:
g.V().has('person', 'name', 'Julia Child').optional(out('knows'))