MCPcopy Create free account
hub / github.com/Graphify-Labs/graphify / _node_link_payload

Function _node_link_payload

graphify/callflow_html.py:222–262  ·  view source on GitHub ↗

Read current graphify graph.json via NetworkX's node-link parser.

(data: dict)

Source from the content-addressed store, hash-verified

220
221
222def _node_link_payload(data: dict) -> tuple[list, list] | None:
223 """Read current graphify graph.json via NetworkX's node-link parser."""
224 if not isinstance(data.get("nodes"), list):
225 return None
226 if not isinstance(data.get("links"), list) and not isinstance(data.get("edges"), list):
227 return None
228
229 try:
230 # Shared loader normalizes the raw writer's "edges" key to "links"
231 # before parsing; without it an edges-keyed payload raised
232 # KeyError: 'links' and this function silently returned None even
233 # though the shape check above accepts "edges" (#2212).
234 from graphify.paths import load_node_link_graph
235
236 # Force directed/multigraph so the stored caller->callee direction and
237 # parallel edges survive the round-trip; mirrors affected.py,
238 # serve.py and cli.py (#1174) and the directed-view fix for
239 # path/shortest_path (#2487, #2309). graph.json is written with
240 # "directed": false for backward compatibility, so without this
241 # networkx returns an undirected Graph and edge orientation becomes
242 # arbitrary -- which silently swaps the Caller and Callee columns of
243 # the call table and drops parallel edges. The _src/_tgt override
244 # below still wins on legacy marker files.
245 graph = load_node_link_graph({**data, "directed": True, "multigraph": True})
246 except Exception:
247 return None
248
249 nodes = []
250 for node_id, attrs in graph.nodes(data=True):
251 node = dict(attrs)
252 node["id"] = node_id
253 nodes.append(node)
254
255 edges = []
256 for index, (source, target, attrs) in enumerate(graph.edges(data=True), 1):
257 edge = dict(attrs)
258 edge["source"] = edge.get("_src", edge.get("source", source))
259 edge["target"] = edge.get("_tgt", edge.get("target", target))
260 edge.setdefault("id", f"edge_{index}")
261 edges.append(edge)
262 return nodes, edges
263
264
265def load_graph(path: str | Path) -> tuple:

Callers 1

load_graphFunction · 0.85

Calls 2

load_node_link_graphFunction · 0.90
getMethod · 0.45

Tested by

no test coverage detected