Build Type Predicate Expressions
This section outlines the process of creating type predicate expressions within your queries using Cypher® Builder.
A type predicate expression serves to validate the type of a variable, literal, property, or another Cypher expression. It follows the syntax below in Cypher:
<expr> IS :: <TYPE>
For example
movie.title IS :: STRING
isType
Type predicate expressions can be constructed using Cypher.isType
(distinct from the .hasType
method in Relationship). The isType
function takes a Cypher variable and a type specified in Cypher.TYPE
:
Cypher.isType(new Cypher.Variable(), Cypher.TYPE.INTEGER);
var0 IS :: INTEGER
Type predicate expressions can be used in a WHERE
statement, for example:
const node = new Cypher.Node();
const matchClause = new Cypher.Match(node).where(Cypher.isType(node.property("title"), Cypher.TYPE.STRING)).return(node);
MATCH (this0)
WHERE this0.title IS :: STRING
RETURN this0
Using Union types
Union types, for example, INTEGER | STRING
, can be used by passing an array to .isType
:
Cypher.isType(new Cypher.Variable(), [Cypher.TYPE.INTEGER, Cypher.TYPE.STRING]);
var0 IS :: INTEGER | STRING
List Types
List types, for instance, LIST<INTEGER>
, can be created using Cypher.TYPES.list()
:
Cypher.isType(new Cypher.Variable(), Cypher.TYPE.list(Cypher.TYPE.STRING));
var0 IS :: LIST<STRING>
Union types can also be used within a list:
Cypher.isType(new Cypher.Variable(), Cypher.TYPE.list([Cypher.TYPE.STRING, Cypher.TYPE.INTEGER]));
var0 IS :: LIST<STRING | INTEGER>
Type predicate expressions with NOT
It is also possible to verify that a Cypher expression is not of a certain type with the function Cypher.isNotType
:
Cypher.isNotType(new Cypher.Variable(), Cypher.TYPE.INTEGER);
var0 IS NOT :: INTEGER
Type predicate expressions for non-nullable types
Type predicate expressions evaluate to true
in Cypher for NULL
values unless a NOT NULL
is appended. In Cypher Builder, this can be achieved using the .notNull
method:
Cypher.isType(new Cypher.Variable(), Cypher.TYPE.INTEGER).notNull();
var0 IS :: INTEGER NOT NULL
Non-nullable types can also be used within a List type:
Cypher.isType(new Cypher.Variable(), Cypher.TYPE.list(Cypher.TYPE.STRING).notNull());
var0 IS :: LIST<STRING NOT NULL>