Functions

Cypher® Builder implements the most common Cypher Functions, such as coalesce or count. In these cases, you can directly call the provided functions to generate the equivalent Cypher:

const movie = new Cypher.Node();

Cypher.coalesce(movie.property("title"), new Cypher.Param("The Matrix"));
coalesce(this0.title, $param0)

All the available functions also have the correct signature for TypeScript.

By convention, all Cypher functions are available as JavaScript functions, with camelCase naming. This differentiates functions from clauses and other elements of the Cypher Builder — for instance, the function count() and the new Count() subquery.

Exposed functions have the closest possible name to their Cypher counterpart, and reside in the same namespace. For example, this query:

Cypher.apoc.cypher.runFirstColumnSingle()

Is equivalent to the Cypher function:

apoc.cypher.runFirstColumnSingle()

Custom functions

In some cases, you may need to use functions that are not available in Cypher Builder. For instance, if you are using plugins or some of the newest features of Neo4j.

For these cases, you can use the class Function to create custom functions. This example calls isNan as a custom function:

const myFunction = new Cypher.Function("myFunction", [new Cypher.Literal(0)]);
myFunction(0);

The class Function takes 2 parameters:

  • name: the name of the function.

  • arguments (optional): an array with the arguments to pass to the function.

The arguments array supports any kind of Cypher expression, so that more complex arguments can be passed, such as:

const myFunction = new Cypher.Function("myFunction", [Cypher.divide(new Cypher.Literal(0), new Cypher.Param(0))]);
myFunction((0 / $param0))

An instance of a function, such as clauses or procedures, should only be used once. If you need to call the same function twice, you need to create two separate instances.

Reusing custom functions

If you want to use the same function multiple times, you can wrap the custom Function class creation into a JavaScript function:

function myFunction(value) {
    return new Cypher.Function("myFunction", value);
}

This way, the function can now be used in the same fashion as the built-in functions:

myFunction(new Cypher.Literal(0));

If you are using TypeScript, you can also make your custom function type-safe:

function myFunction(value: Cypher.Expr): Cypher.Function {
    return new Cypher.Function("myFunction", value);
}

For more information on Cypher customization in Cypher Builder, see Customize Cypher.

Namespaces

Some functions in Cypher reside in namespaces, such as apoc functions. In this case, set the name of the Function class to the full path of the function, for example:

new Cypher.Function("apoc.agg.first");