Python-Native Query API

Instead of a standalone string-based query language, CogDB provides a Python-native query system called Torque. If you are comfortable writing Python, you can start querying graphs right away.

The Torque API

Torque allows you to traverse your data using a fluent and chainable API that feels completely natural to Python developers.

from cog.torque import Graph

g = Graph("people")
g.put("alice", "follows", "bob")
g.put("bob", "follows", "fred")
g.put("bob", "status", "cool_person")
g.put("greg", "status", "cool_person")

# Who do cool people follow?
g.v().has("status", "cool_person").out("follows").unique().all()
# {'result': [{'id': 'fred'}, {'id': 'bob'}, {'id': 'greg'}]}

Dynamic and Composable

Instead of string-concatenating complex query logic, Torque traversals are just Python objects. You can build up queries step-by-step, conditionally add traversal steps, or pass the query object around your application before executing it.

# Build a query dynamically
query = g.v("alice").out("follows")

if include_second_degree:
    query = query.out("follows")

# Execute when ready
results = query.all()

IDE Support and Safety

Because Torque is standard Python, you benefit from native IDE support, autocomplete, and syntax highlighting. It also means you can easily compose queries dynamically based on application logic without risking query injection.