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. Returning
the reached nodes directly is the only way the question is answerable at all.
The anchor value always travels as the query parameter $anchor_id, never in
the query text: it comes from a request, and interpolating it would let a caller
rewrite the query.
Attributes¶
ANCHOR_PARAM = 'anchor_id'
module-attribute
¶
logger = logging.getLogger(__name__)
module-attribute
¶
Classes¶
Functions:¶
cypher_graph_neighbors(conn, *, vertex_type, key, hops, direction, edge_types, limit, schema, run)
¶
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 — change → server → application → service — 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 single-field mapping naming a declared property. |
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 |
Returns:
| Name | Type | Description |
|---|---|---|
GraphContainer |
GraphContainer
|
reached vertices, keyed by logical type. |
Source code in graflo/db/cypher/traversal.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 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 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 | |
cypher_neighbors_query(*, anchor_label, anchor_key_field='id', edge_type, far_label, direction, hops, limit)
¶
Render a bounded neighbourhood query.
The anchor value is not part of the text: bind it as the $anchor_id
parameter when running the query.
Returns the reached nodes, their distance and their labels, deduplicated.
DISTINCT is load-bearing: a graph with a cycle reaches the same node by
several paths, and without it the row count 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
|
Property the anchor is matched on. |
'id'
|
edge_type
|
str | None
|
Relationship type to follow, several joined with |
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 |