Types
TypeQL is a polymorphic query language that supports strong typing and uses types defined in the schema of a database to construct queries to the database.
Type hierarchy
Types are defined in the schema of a database, and their definitions are written in TypeQL. Types from the schema are used to design data queries to the database and are modeled with the use of the PERA model. Type definitions can be used in Define and Undefine queries and can’t use any variables.
To create new types in a schema, subtype one of the existing types. A subtype inherits all definitions of its supertype.
By default, a schema has three built-in root types. Root types are abstract types and need to be subtyped to use them.
Root type label | A subtype of the root type | A data instance of a subtype |
---|---|---|
|
Entity type |
Entity |
|
Relation type |
Relation |
|
Attribute type |
Attribute |
Since data instances can be named after their respective root type, we try to prevent ambiguity by specifying whether we mean the root type or data instance in every case if it’s not obvious from the context.
Abstract types
Abstract types can’t be used to insert data instances, but can be subtyped and queried for.
To make a type abstract, use the abstract
keyword in its definition.
define
subject sub entity, abstract;
Usage in a Define query
Let’s define a simple schema for users:
define
id sub attribute, value long;
email sub attribute, value string;
full-name sub attribute, value string;
user sub entity,
owns id @key,
owns email,
abstract;
person sub user, owns full-name;
Usage in a Fetch query
Now let’s fetch all types from the database:
match
$x sub! $y;
fetch
$x;
This simple pattern lets us match all types from a database:
{ "x": { "label": "user", "root": "entity" } }
{ "x": { "label": "entity", "root": "entity" } }
{ "x": { "label": "attribute", "root": "attribute", "value_type": "object" } }
{ "x": { "label": "relation", "root": "relation" } }
{ "x": { "label": "person", "root": "entity" } }
{ "x": { "label": "email", "root": "attribute", "value_type": "string" } }
{ "x": { "label": "full-name", "root": "attribute", "value_type": "string" } }
{ "x": { "label": "id", "root": "attribute", "value_type": "long" } }