TypeQL language guide
The TypeQL 3.0 language guide is currently under construction. More material will be published soon. Join our Discord chat server to stay updated. |
This page gives an overview of the TypeQL query language. For an overview of the data model on which TypeQL queries are built see here.
Queries and functions
The main way of interacting with your TypeDB database is through queries written in TypeQL, the query language of TypeDB. Queries fall into two categories.
-
Schema queries comprise a single stage, indicated by the corresponding keywords shown in the table below. Schema queries modify the schema of your database, and produce no outputs except for a confirmation of success.
-
Data pipelines may comprise multiple stages, indicated by the keywords shown in the table below. Data pipelines read and write data from the database, and always output of the read or written data (though the set of outputs may be empty).
Table 1 also mentions functions: functions are data pipeline templates that take typed input arguments. This allows users to build powerful query abstractions. Functions cannot write or delete data, and thus they cannot contain write stages like insert
or delete
.
Query type | Stage keywords | Multi-stage? |
---|---|---|
Schema queries |
|
no |
Data pipelines |
… |
yes |
Functions |
… |
yes |
Statements and patterns
The body of a stage comprises statements. For example:
-
The body of a
define
stage comprises statements such asuser owns username
. -
The body of a
match
stage comprises statements such as$u has username $n
or$type owns username
.
Which statements can be used depends on the stage we are in.
A key distinction between stages and their statements is whether statements contain variables or not.
-
Statements without variables are called definitions.
-
Statements with variables are called (elementary) patterns.
Patterns can be combined into larger patterns by pattern operations, such as conjunction (“chaining”), disjunction (“branching”), negation (“exclusion”), etc.
Definition vs pattern statements
Definitions and patterns play distinct roles in TypeQL.
-
Schema queries always work with (lists of) definitions.
-
Data pipelines (and functions) always work with patterns.
As a simple example of this distinction, compare:
define
entity user, plays friendship:friend;
relation friendship, relates friend @card(2);
with
match
$some-type plays $some-role;
friendship relates $some-role;
The former query introduces types, while the latter will query for types. The general idea here is as follows.
Definition statements
Definitions in schema queries concern:
-
type definitions, introducing new types via the appropriate keyword (
entity
,relation
+relates
,attribute
+value
). Types are referred to by a unique type labels; multiple aliases of the latter can be introduced (coming soon). -
trait definitions, introducing player or owner traits (via
plays
andowns
keywords). Player traits allow type instances to be referred to as players in roles of relations. Owner traits allow type instances to be referred to as -
subtyping definitions, declare types to be subtypes of other types (via the keyword
sub
). Traits of supertypes are inherited by subtypes.
Pattern statements
In contrast, in patterns we support a wide variety of syntax, including for:
-
Retrieving types based on label, traits, and inheritance.
-
Retrieving data in types, e.g. based on references between data instance such as attribute ownerships and relation linkages (via keywords such as
has
,links
, and anonymous versions thereof) -
Function calls (
let $a, $b in <FUN-CALL>
) -
Computation with values (
let $x = <EXPR>
) -
… and many more.
Expressions and value types
TypeQL supports various build in operators to manipulate and combine data types, which can be used in the construction of statements (such as assignments and comparisons).
Index and glossary
For a complete list of TypeQL keywords used at various levels of query construction, consult the keyword glossary.