TypeDB 3.0 is live! Get started for free.

Query answers

This page gives a brief overview of answer types returned by the server when running queries.

Query and answer types

Data pipelines produce query answers. Currently TypeDB supports two answers formats:

  1. Concept rows (the native TypeDB format). Any valid data pipeline not ending with a fetch stage will produce answers in concept row format.

  2. JSON documents. A data pipeline ending with a fetch stage will produce JSON answers.

In contrast, schema queries do not produce answers, but only return success/failures notifications.

Answers in clients

Concept row answers

Start a transaction of the desired type (read, write, or schema):

> transaction sample_db read
sample_db::read>
bash

When running a query in TypeDB Console, each concept row is printed as a table where the left column is the variable name, and the right is the variable’s value in the row.

Sample query and output
sample_db::read> match
                   $user1 has username $name1;
                   $user2 has username $name2;
                   friendship ($user1, $user2);
                   $name1 < $name2;

Finished validation and compilation...
Streaming answers...

   ------------
    $name1 | user1 isa username
    $name2 | user2 isa username
    $user1 | iid 0x1e00000000000000000014 isa user
    $user2 | iid 0x1e00000000000000000015 isa user
   ------------
    $name1 | user1 isa username
    $name2 | user3 isa username
    $user1 | iid 0x1e00000000000000000014 isa user
    $user2 | iid 0x1e00000000000000000016 isa user
   ------------

Finished. Total answers: 2
bash

If you made any changes and are ready to commit, simply type commit:

sample_db::write> commit
Transaction changes committed
bash

If you wish to close the transaction without committing instead, you can press Ctrl+C (interrupt) or Ctrl+D (end of input).

JSON answers

Open a transaction of the desired type (read, write, or schema):

> transaction sample_db read
sample_db::read>
bash

Each output is printed as a valid JSON object with the format specified by the fetch query.

Sample query and output
test::read> match
                $user1 has username $name1;
                $user2 has username $name2;
                friendship ($user1, $user2);
                $name1 < $name2;
            fetch {
                "friend1": $name1,
                "friend2": $name2,
            };

Finished validation and compilation...
Streaming documents...

{
    "friend1": "user1",
    "friend2": "user2"
}
{
    "friend1": "user1",
    "friend2": "user3"
}

Finished. Total answers: 2
bash

Answers in drivers

Concept rows answers

Follow the connection guide to connect the driver to a TypeDB server.

Open a transaction to the selected database and use the transaction.query() method to send the query:

let transaction = driver.transaction(DB_NAME, TransactionType::Read).await.unwrap()?;
let query = "
  match
    $user1 has username $name1;
    $user2 has username $name2;
    friendship($user1, $user2);
    $name1 < $name2;
";
let answers = transaction.query(query).await?;
rust

Where answers is a QueryAnswer holding the ConceptRowHeader with the output variable names for reference, as well as a stream of ConceptRows. Each ConceptRow is a mapping from variable names (not including the $ sigil) to their values in the row.

JSON answers

Follow the connection guide to connect the driver to a TypeDB server.

Open a transaction to the selected database and use the transaction.query() method to send the query:

let transaction = driver.transaction(DB_NAME, TransactionType::Read).await.unwrap()?;
let query = "
  match
    $user1 has username $name1;
    $user2 has username $name2;
    friendship($user1, $user2);
    $name1 < $name2;
  fetch {
      "friend1": $name1,
      "friend2": $name2,
  };
";
let answers = transaction.query(query).await?;
rust

Where answers is a QueryAnswer holding the stream of ConceptDocuments. Each ConceptDocument is equivalent to a JSON and can be serialized as such.