Skip to content

GraphQuery API

The query object is available in every custom rule context. It provides high-level methods for inspecting the import graph. All glob patterns match against paths relative to rootDir.

MethodReturnsDescription
edgesFrom(glob)EdgeQueryResultEdges originating from files matching the glob.
edgesTo(glob)EdgeQueryResultEdges targeting files matching the glob.
edgesBetween(fromGlob, toGlob)EdgeQueryResultEdges from one set of files to another.

Each EdgeQueryResult has:

  • edges — the matched DependencyEdge[]
  • violations(code, message) — converts matched edges into violation results. message can be a string or a (edge) => string function for dynamic messages.
MethodReturnsDescription
filesMatching(glob)string[]All graph nodes matching the glob.
featureOf(file)string | nullThe feature owning a file, or null.
importsOf(file)string[]Direct imports (outgoing neighbors).
importersOf(file)string[]Direct importers (incoming neighbors).
MethodReturnsDescription
dependsOn(file, target)booleanWhether file transitively depends on target.
dependenciesOf(file)string[]All transitive dependencies of a file.
dependentsOf(file)string[]All transitive reverse dependencies.
MethodReturnsDescription
fanOut(file)numberNumber of direct imports.
fanIn(file)numberNumber of direct importers.

The raw DependencyGraph is also available via context.graph:

  • nodes (Set<string>) — all analyzed file paths
  • edges (DependencyEdge[]) — all import edges, each with from, to, and importInfo (specifier, line, col, isDynamic, isTypeOnly)
  • adjacencyList (Map<string, Set<string>>) — for fast neighbor lookups