graflo.db.traversal¶
Backend-neutral multi-hop traversal, composed from single-hop primitives.
Every backend that can answer fetch_edges gets correct multi-hop semantics
from this module, in one shape: a
:class:~graflo.architecture.graph_types.container.GraphContainer. Backends with
a native multi-hop query override :meth:Connection.graph_neighbors for a single
round trip, and the conformance suite asserts the override agrees with this
default rather than merely "returning something".
Direction is decided per edge, not per request: an edge declared
directed=False is followed both ways whatever the caller asked for, and a
backend that physically cannot follow the requested direction fails loudly
before any query runs, rather than returning a partial neighbourhood.
Attributes¶
DEFAULT_EDGE_LIMIT = 1000
module-attribute
¶
logger = logging.getLogger(__name__)
module-attribute
¶
Classes¶
Functions:¶
bfs_neighbors(conn, *, anchor_type, anchor_key, hops=1, direction=EdgeDirection.OUT, edge_types=None, filters=None, limit=None, schema=None)
¶
Breadth-first neighbourhood around one anchor, as a GraphContainer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
conn
|
Connection
|
Live connection. Only |
required |
anchor_type
|
str
|
Logical vertex type of the anchor. |
required |
anchor_key
|
str | dict[str, Any]
|
Anchor identity value, or a field mapping to resolve. |
required |
hops
|
int
|
Maximum hop distance, at least 1. |
1
|
direction
|
EdgeDirection
|
Orientations followed from each frontier vertex. |
OUT
|
edge_types
|
Sequence[str] | None
|
Restrict to these logical relation names. A declared inverse that stores nothing is a valid name: it reads the forward edge from its target, and the result reports it under the name asked for, endpoints in that reading's order. |
None
|
filters
|
Any | None
|
Optional edge filter, rendered per backend dialect. |
None
|
limit
|
int | None
|
Maximum accumulated edges. Defaults to |
None
|
schema
|
Schema | None
|
Required — logical names must be resolved to storage names, or the "universal layer" leaks backend naming to the caller. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
GraphContainer |
GraphContainer
|
reached vertices and edges, deduplicated. |
Raises:
| Type | Description |
|---|---|
ValueError
|
if hops < 1, schema is missing, or anchor_type is not declared in the schema. |
UnsupportedEdgeDirectionError
|
if a backend cannot follow an edge in the requested direction. |
Source code in graflo/db/traversal.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 | |
check_anchor_fields(schema, db_aware, vertex_type, fields)
¶
Refuse an anchor key that names a property vertex_type does not declare.
A native traversal writes the anchor's field name into its query text, and the name arrives with the request, so an undeclared one is not merely a miss: it is an injection point. Checking against the schema closes it for every backend at once.
Raises:
| Type | Description |
|---|---|
ValueError
|
naming the undeclared field and the declared ones. |
Source code in graflo/db/traversal.py
edge_query_name(db_aware, edge, flavor)
¶
The identifier a backend's read path uses for edge.
Backends name edge types in incompatible ways, and no single accessor covers
them: edge_storage_name is Arango-only by construction (it builds an edge
collection name and returns None elsewhere), the Cypher family and
Nebula key on the relation type, and PostgreSQL keys on a derived table name.
Resolving that here keeps the choice in one place instead of in every caller.
Source code in graflo/db/traversal.py
normalize_edge_row(row)
¶
Reduce a backend's edge row to (properties, source key, target key).
fetch_edges predates this wave and returns whatever shape each driver
finds natural: Arango yields an edge document with _from/_to, the
Cypher family yields the driver's (start props, type, end props) triple,
PostgreSQL yields source_id/target_id columns. Normalizing here is
what lets every backend answer in one GraphContainer without changing a
read path other code already depends on.
Source code in graflo/db/traversal.py
select_edges(schema, edge_types)
¶
Declared edges a walk may follow, each with the inverse name it is read through.
With no allow-list every declared edge is followed as stored. A relation
name is resolved through the declared inverses
(:func:~graflo.architecture.schema.inverse_realization.resolve_relation):
a name that labels declared edges selects them as stored, and a name that is
only the declared inverse of one -- nothing stored under it -- selects those
edges read backwards. The second element is that inverse name, or None
for an edge read as stored.