abinazebinoy commited on
Commit
7beb5e9
·
1 Parent(s): d27030a

fix(api): BUG-10 -- deduplicate graph edges before sending to frontend

Browse files

With depth=2, Neo4j returns many paths sharing intermediate edges.
Each shared edge appeared in multiple row[rels] arrays and was
appended to a plain list unconditionally. D3 force simulation
received duplicate links creating visual double/triple edges and
distorting the force calculation (artificially stronger pull).

Fix: track seen (src, tgt, type) tuples in _edge_seen set and
skip duplicates.

Files changed (1) hide show
  1. api/routes/graph.py +9 -4
api/routes/graph.py CHANGED
@@ -21,6 +21,7 @@ def get_connections(
21
 
22
  nodes = {}
23
  edges = []
 
24
 
25
  with driver.session() as session:
26
  # Always load anchor node first — guarantees non-empty response
@@ -84,10 +85,14 @@ def get_connections(
84
  tgt = rel_step.get("tgt", "")
85
  rtype = rel_step.get("type", "RELATED_TO")
86
  if src and tgt:
87
- edges.append(GraphEdge(
88
- source=src, target=tgt, relationship=rtype,
89
- properties={},
90
- ))
 
 
 
 
91
 
92
  return GraphResponse(
93
  entity_id=entity_id,
 
21
 
22
  nodes = {}
23
  edges = []
24
+ _edge_seen = set() # BUG-10 FIX: dedup (src, tgt, type) triples
25
 
26
  with driver.session() as session:
27
  # Always load anchor node first — guarantees non-empty response
 
85
  tgt = rel_step.get("tgt", "")
86
  rtype = rel_step.get("type", "RELATED_TO")
87
  if src and tgt:
88
+ # BUG-10 FIX: skip duplicate edges -- depth>1 paths share edges
89
+ edge_key = (src, tgt, rtype)
90
+ if edge_key not in _edge_seen:
91
+ _edge_seen.add(edge_key)
92
+ edges.append(GraphEdge(
93
+ source=src, target=tgt, relationship=rtype,
94
+ properties={},
95
+ ))
96
 
97
  return GraphResponse(
98
  entity_id=entity_id,