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

  • Console

  • Studio

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

> transaction sample_db read
sample_db::read>

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

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

sample_db::write> commit
Transaction changes committed

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

Follow the Studio manual to connect to TypeDB and select a project folder. Then use the steps below:

  1. Use a drop-down list in the top toolbar to select a database.

  2. Select the desired transaction type.

  3. Open a new tab and insert or type in an insert stage, for example:

  4. Run the query by clicking the studio run Run Query button.

  5. Commit the changes by clicking the studio check Commit query button.

Sample query
match
  $user1 has username $name1;
  $user2 has username $name2;
  friendship($user1, $user2);
  $name1 < $name2;

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 output
## Running>
match
  $user1 has username $name1;
  $user2 has username $name2;
  friendship($user1, $user2);
  $name1 < $name2;

## Result> Success.
    $name1 | user1 isa username
    $name2 | user2 isa username
    $user1 | iid 0x1e00000000000000000011 isa user
    $user2 | iid 0x1e00000000000000000012 isa user
   ------------
    $name1 | user1 isa username
    $name2 | user3 isa username
    $user1 | iid 0x1e00000000000000000011 isa user
    $user2 | iid 0x1e00000000000000000013 isa user
   ------------
## Completed

JSON answers

  • Console

  • Studio

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

> transaction sample_db read
sample_db::read>

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

Follow the Studio manual to connect to TypeDB and select a project folder. Then use the steps below:

  1. Use a drop-down list in the top toolbar to select a database.

  2. Select the desired transaction type.

  3. Open a new tab and insert or type in an insert stage, for example:

  4. Run the query by clicking the studio run Run Query button.

  5. Commit the changes by clicking the studio check Commit query button.

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

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

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

## Result> Success.
{
    "friend1": "user1",
    "friend2": "user2"
}
{
    "friend1": "user1",
    "friend2": "user3"
}
## Completed

Answers in drivers

Concept rows answers

  • Rust

  • Python

  • Java

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?;

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.

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:

with driver.transaction(DB_NAME, TransactionType.READ) as tx:
    query = """
      match
        $user1 has username $name1;
        $user2 has username $name2;
        friendship($user1, $user2);
        $name1 < $name2;
    """
    answers = tx.query(insert_query).resolve()

Where answers is an iterator yielding ConceptRows, where each concept row is effectively a mapping from variable names (not including the $ sigil) to their values in the row.

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:
try (TypeDBTransaction tx = driver.transaction(DB_NAME, Transaction.Type.READ)) {
    String query = """
      match
        $user1 has username $name1;
        $user2 has username $name2;
        friendship($user1, $user2);
        $name1 < $name2;
    """;
    ConceptRowsIterator answers = tx.query(query).resolve().asConceptRows();
}

Where answers is an iterator yielding ConceptRows, where each concept row is effectively a mapping from variable names (not including the $ sigil) to their values in the row.

JSON answers

  • Rust

  • Python

  • Java

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?;

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

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:
with driver.transaction(DB_NAME, TransactionType.READ) as tx:
    query = """
      match
        $user1 has username $name1;
        $user2 has username $name2;
        friendship($user1, $user2);
        $name1 < $name2;
      fetch {
          "friend1": $name1,
          "friend2": $name2,
      };
    """
    answers = tx.query(insert_query).resolve()

Where answers is an iterator yielding ConceptDocuments. Each ConceptDocument is equivalent to a JSON and can be serialized as such.

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:
try (TypeDBTransaction tx = driver.transaction(DB_NAME, Transaction.Type.READ)) {
    String query = """
      match
        $user1 has username $name1;
        $user2 has username $name2;
        friendship($user1, $user2);
        $name1 < $name2;
    """;
    ConceptDocumentsIterator answers = (ConceptRowIterator)tx.query(query).resolve().asConceptDocuments();
}

Where answers is an iterator yielding ConceptDocuments. Each ConceptDocument is equivalent to a JSON and can be serialized as such.