graflo.db.cypher.traversal¶
Native variable-length traversal for the Cypher family.
Neo4j, Memgraph and FalkorDB all express a bounded neighbourhood as a single
variable-length pattern, so one builder serves all three — the same reason
:func:cypher_rel_pattern is shared.
A native override is not only a performance choice here. fetch_edges on this
family returns RETURN r, and a driver renders a bare relationship without its
endpoints, so the generic breadth-first default has nothing to walk to. The query
returns the reached nodes and the relationships of the paths that reached them,
and both are shaped as the default would return them: vertices by type, edge rows
by edge id with their ends under source and target.
Anchor values always travel as query parameters ($anchor_id, then
$anchor_id_1 … for a composite key), never in the query text: they come from
a request, and interpolating them would let a caller rewrite the query. Filter and guard values travel as bound parameters too.
An edge filter holds for every relationship of a path, and a guard for every node and relationship of it, the anchor included: a path through a hidden element reaches nothing.
Attributes¶
ANCHOR_PARAM = 'anchor_id'
module-attribute
¶
NEIGHBOR_COLUMNS = ('far', 'labels', 'edges')
module-attribute
¶
logger = logging.getLogger(__name__)
module-attribute
¶
Classes¶
Functions:¶
anchor_param(position)
¶
Name of the query parameter binding the anchor's position-th key field.
cypher_graph_neighbors(conn, *, vertex_type, key, hops, direction, edge_types, limit, schema, run, filters=None, guard=None)
¶
Run a bounded neighbourhood query and shape it as a GraphContainer.
Shared by Neo4j, Memgraph and FalkorDB; each supplies run, which is the only thing that differs between their drivers.
One hop fans out one pattern per relation that touches the anchor type. More than one hop issues a single pattern over every allowed relation at once, so a walk can cross relation types — work order → machine → line → product — which a pattern per relation never could. The reached node's type is then read from its labels.
Direction is decided per edge, exactly as in the backend-neutral default
(:func:graflo.db.traversal.bfs_neighbors): an edge declared
directed: false is followed both ways whatever direction says. A
variable-length pattern has one direction for all its relationship types, so
when the selected relations disagree the walk falls back to one-hop patterns
issued hop by hop.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
conn
|
Connection
|
The live connection, for flavor-aware name resolution. |
required |
vertex_type
|
str
|
Logical anchor type. |
required |
key
|
str | dict[str, Any]
|
Anchor identity value, or a mapping of declared properties to match, every one bound (a composite identity names them all). |
required |
hops
|
int
|
Maximum hop distance. |
required |
direction
|
EdgeDirection
|
Orientation followed from the anchor. |
required |
edge_types
|
Sequence[str] | None
|
Logical relation names to restrict to; None means every relation. A declared inverse that stores nothing reads the forward edge from its target. |
required |
limit
|
int | None
|
Maximum reached nodes. |
required |
schema
|
Schema | None
|
Required for logical -> storage naming. |
required |
run
|
Callable[[str, dict[str, Any]], list[dict[str, Any]]]
|
Executes a query string with its parameters and returns rows as
dicts carrying |
required |
filters
|
Any | None
|
Edge filter, in stored names, that every relationship of a path must satisfy. |
None
|
guard
|
FilterExpression | None
|
Trusted predicate, in stored names, that every node and relationship of a path must satisfy. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
GraphContainer |
GraphContainer
|
reached vertices, keyed by logical type. |
Source code in graflo/db/cypher/traversal.py
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 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 | |
cypher_neighbors_query(*, anchor_label, anchor_key_field='id', edge_type, far_label, direction, hops, limit, conditions=())
¶
Render a bounded neighbourhood query.
The anchor values are not part of the text: bind the first as
$anchor_id and each further one as $anchor_id_<n>
(:func:anchor_param) when running the query.
Returns one row per reached node: its properties (far), its labels, and
the relationships of every path that reached it (edges: type,
properties, and each end's properties and labels). Grouping by the node
is load-bearing: a graph with a cycle reaches the same node by several
paths, and without it the row count, and what LIMIT counts, grows with
path multiplicity rather than with neighbourhood size. The anchor itself is
excluded, since a walk in both directions can come back to it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
anchor_label
|
str
|
Storage label of the anchor node. |
required |
anchor_key_field
|
str | Sequence[str]
|
Property, or properties, the anchor is matched on. |
'id'
|
edge_type
|
str | Sequence[str] | None
|
Relationship type to follow, several to follow any of them, or None for any type. |
required |
far_label
|
str | None
|
Storage label the reached node must carry, or None for any. |
required |
direction
|
EdgeDirection
|
Orientation followed from the anchor. |
required |
hops
|
int
|
Maximum hop distance. |
required |
limit
|
int | None
|
Maximum reached nodes. |
required |
conditions
|
Sequence[str]
|
Further predicates over |
()
|
Source code in graflo/db/cypher/traversal.py
path_conditions(params, *, filters=None, guard=None)
¶
The WHERE predicates an edge filter and a guard put on path.
Values are bound into params.