The path() method finds the shortest path between two nodes in the graph, regardless of edge direction. This is useful for understanding how two components are connected and discovering unexpected dependencies or relationships.The method uses graph shortest path algorithms to efficiently find the minimal connection path.
from graph.query import QueryEnginequery_engine = QueryEngine(storage)# Find how frontend connects to a databasepath_result = query_engine.path( from_id="application:web-frontend", to_id="database:users-db")print(f"Path length: {path_result['path_length']} hops")print(f"\n{path_result['path_description']}")
# Check if a service has an unexpected dependencyfrontend_to_db = query_engine.path( from_id="application:mobile-app", to_id="database:internal-admin-db")if path_result['path_length'] > 0: print("⚠️ WARNING: Mobile app has path to internal admin database!") print(f"\nPath: {path_result['path_description']}") print("\nThis may indicate:") print(" - Architectural violation") print(" - Security concern") print(" - Missing API gateway")else: print("✓ No direct path found (good)")
# Find paths from a service to multiple databasesservice_id = "service:analytics-processor"databases = [ "database:events-db", "database:analytics-db", "database:users-db"]print(f"Paths from {service_id}:\n")for db_id in databases: path_result = query_engine.path(service_id, db_id) if path_result['path_length'] > 0: print(f"To {path_result['nodes'][-1]['name']}:") print(f" Length: {path_result['path_length']} hops") print(f" Via: {' → '.join([n['name'] for n in path_result['nodes'][1:-1]])}") else: print(f"To {db_id}: No path") print()