Officially out now: The TypeDB 3.0 Roadmap >>

Lesson 3.5: Query validation

TypeQL’s composability provides a natural syntax for building queries, but not all queries are valid. Try running the following queries. Only the first one will be successfully executed.

match
$user isa user;
$country isa country;
(location: $country, located: $user) isa locating;
fetch
$user: name;
$country: name;

studio run Run

match
$user isa user;
$country isa country;
(location: $country, located: $user) isa locating;
fetch
$user: isbn-13;
$country: name;

studio run Try running

match
$book isa book;
$country isa country;
(location: $country, located: $book) isa locating;
fetch
$book: isbn-13;
$country: name;

studio run Try running

The second query is invalid because users do not have ISBN attributes. The third is invalid because books do not have locations. TypeDB validates queries against the schema, which sets out which types are able to own each attribute and play each role in relations. This enables the database to determine which queries are semantically sound, and extends to queries of arbitrary complexity, for instance the following query. Try running it to see if it is valid.

match
(located: $user, location: $user-city) isa locating;
(action: $order, executor: $user) isa action-execution;
(delivered: $order, destination: $destination) isa delivery;
(located: $destination, location: $destination-city) isa locating;
$user-city isa city;
$destination-city isa city;
not { $destination-city is $user-city; };
fetch
$order: id;

studio run Try running

We will explore exactly how TypeDB validates queries in Lesson 7.1.

Exercise

Determine what the above query is asking for.

Answer

It retrieves the IDs of orders being sent to a city other than the city of the user that placed the order. Perhaps these orders are gifts! This query uses two keywords we haven’t introduced yet: not and is. We will learn about these keywords in Lesson 7.3.