Skip to content

Advanced Queries

OWLGraph extends Dgraph’s DQL query language with ontology-aware features. This guide covers the key query patterns.

The type() function works as a subsumption query because ancestor types are materialized:

# All animals (Dogs, Cats, Birds, everything under Animal)
{ q(func: type(Animal)) { name dgraph.type } }
# All dogs (GoldenRetrievers, Labradors, any Dog subclass)
{ q(func: type(Dog)) { name breed } }
# Only golden retrievers
{ q(func: type(GoldenRetriever)) { name breed } }

exactType() matches only nodes where the type was directly asserted, not inferred:

# Only nodes explicitly typed as Dog (not GoldenRetriever or Labrador)
{ q(func: exactType(Dog)) { name } }

A node typed as GoldenRetriever matches type(Dog) but does not match exactType(Dog).

Follow a transitive property until no more edges exist using the * suffix:

# Follow locatedIn transitively (City -> Country -> Continent -> ...)
{
q(func: eq(name, "San Francisco")) {
name
locatedIn* {
name
dgraph.type
}
}
}

Limit traversal depth with *N:

# Follow locatedIn up to 2 hops
{
q(func: eq(name, "San Francisco")) {
name
locatedIn*2 {
name
}
}
}

All materialized edges carry an owl.inferred=true facet:

# Show which types are inferred vs. asserted
{
q(func: type(Dog)) {
name
dgraph.type @facets(owl.inferred)
}
}

OWLGraph queries work with all standard DQL features:

{
q(func: type(Animal), first: 10, offset: 20, orderasc: name) {
name
dgraph.type
}
}
{
q(func: type(Book)) @filter(eq(testament, "Old")) {
name
reference
}
}
{
count(func: type(Prophecy)) {
count(uid)
}
}
{
q(func: type(Prophecy)) {
reference
text
hasTheme { name }
fulfilledBy {
reference
inBook { name testament }
}
}
}

Because OWLGraph materializes inferences at write time, subsumption queries are just index lookups — they’re as fast as any type() query in standard Dgraph. There is no runtime reasoning overhead.

Transitive property paths (pred*) do traverse edges at query time, so they scale with the depth of the traversal. Use bounded paths (pred*N) when you know the maximum depth.