Advanced Queries
OWLGraph extends Dgraph’s DQL query language with ontology-aware features. This guide covers the key query patterns.
Subsumption Queries
Section titled “Subsumption Queries”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 } }Exact Type Queries
Section titled “Exact Type Queries”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).
Transitive Property Paths
Section titled “Transitive Property Paths”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 } }}Bounded Transitive Paths
Section titled “Bounded Transitive Paths”Limit traversal depth with *N:
# Follow locatedIn up to 2 hops{ q(func: eq(name, "San Francisco")) { name locatedIn*2 { name } }}Filtering by Inferred vs. Asserted
Section titled “Filtering by Inferred vs. Asserted”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) }}Combining with DQL Features
Section titled “Combining with DQL Features”OWLGraph queries work with all standard DQL features:
Pagination
Section titled “Pagination”{ q(func: type(Animal), first: 10, offset: 20, orderasc: name) { name dgraph.type }}Filtering
Section titled “Filtering”{ q(func: type(Book)) @filter(eq(testament, "Old")) { name reference }}Aggregation
Section titled “Aggregation”{ count(func: type(Prophecy)) { count(uid) }}Nested Queries
Section titled “Nested Queries”{ q(func: type(Prophecy)) { reference text hasTheme { name } fulfilledBy { reference inBook { name testament } } }}Query Performance
Section titled “Query Performance”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.