How to Visualize the Generated Code Graph in code-graph-rag
To visualize the generated code graph, export your codebase to JSON using the cgr build command, ingest it into Memgraph or Neo4j via the MemgraphIngestor class, and execute the Cypher procedure CALL db.schema.visualization(); in the database's web interface.
The vitali87/code-graph-rag project constructs a language-agnostic graph representation that maps the structural and semantic relationships within your codebase. Once you generate this graph, you need to visualize the generated code graph to understand architectural dependencies, inheritance chains, and call patterns. The repository provides native integration with Cypher-compatible databases to render interactive schema diagrams directly from the exported JSON structure.
Export the Graph to JSON
The first step is generating the JSON export that contains your codebase's graph structure. The CLI entry point in codebase_rag/main.py provides the cgr command, which internally uses codebase_rag.graph_updater.GraphUpdater to traverse your source tree.
Run the following command from your project root:
cgr build --project . --output my_project.graph.json
This produces a JSON file following the internal schema defined in codebase_rag.constants.graph. The export contains nodes (functions, classes, modules) and relationships (calls, imports, inheritance) required for visualization.
Ingest the Graph into Memgraph or Neo4j
To visualize the graph, you must load the JSON into a running graph database. The repository ships with database connectors in codebase_rag/services/graph_service.py.
Use codebase_rag.graph_loader.load_graph() to parse the JSON file, then stream it into Memgraph using the MemgraphIngestor class:
from pathlib import Path
from codebase_rag.graph_loader import load_graph
from codebase_rag.services.graph_service import MemgraphIngestor
# Load the JSON export into a GraphLoader object
graph = load_graph(Path("my_project.graph.json"))
# Ingest into a running Memgraph instance
with MemgraphIngestor(host="localhost", port=7687) as ingestor:
ingestor.ingest(graph)
Tip: Memgraph ships with a web UI called Memgraph Lab accessible at http://localhost:3000. Neo4j users can substitute the MemgraphIngestor with the Neo4j equivalent class to use Neo4j Browser instead.
Visualize the Schema with Native Cypher
Once ingestion completes, execute the built-in schema visualization procedure:
CALL db.schema.visualization();
Both Memgraph and Neo4j expose this procedure natively. In Memgraph Lab or Neo4j Browser, this command renders a diagram where each node label (e.g., Function, Class) appears as a colored box and each relationship type (e.g., CALLS, IMPORTS) appears as a directed arrow linking the boxes.
This schema view provides the high-level topology of your codebase without requiring custom visualization code.
Explore the Graph Interactively
After viewing the schema diagram, run ad-hoc Cypher queries to inspect specific components:
# List top-level functions
MATCH (f:Function)
RETURN f.name
LIMIT 10;
# Trace call relationships between functions
MATCH (caller:Function)-[r:CALLS]->(callee:Function)
RETURN caller.name, callee.name, r.type
LIMIT 25;
These queries execute directly in the same database UI, allowing you to drill down from the macro-level schema view into specific dependency chains.
Summary
codebase_rag/graph_loader.pyprovidesload_graph()to parse the JSON export into a Python object.codebase_rag/services/graph_service.pycontainsMemgraphIngestorto stream the graph into a live database.CALL db.schema.visualization()is the native Cypher procedure that renders the interactive schema diagram in Memgraph Lab or Neo4j Browser.- The example script in
examples/graph_export_example.pydemonstrates loading a graph file and printing a summary before database ingestion.
Frequently Asked Questions
What file format does code-graph-rag use for the graph export?
The repository generates a JSON file that follows the schema defined in codebase_rag.constants.graph. This file contains nodes representing functions, classes, and modules, along with edges representing calls, imports, and inheritance relationships. You load this file via load_graph() before ingesting it into your chosen database.
Can I visualize the code graph without installing Memgraph or Neo4j?
No, the visualization pipeline depends on the built-in Cypher procedure CALL db.schema.visualization(), which is only available in Memgraph or Neo4j. You must ingest the JSON graph into one of these databases using the provided ingestor classes to render the interactive diagram.
Where is the CLI entry point located in the source code?
The CLI is defined in codebase_rag/main.py, which exposes the cgr build command. This entry point internally instantiates codebase_rag.graph_updater.GraphUpdater to walk the directory tree, parse source files, and emit the JSON export specified by the --output flag.
How do I filter which relationships appear in the visualization?
While CALL db.schema.visualization() shows all node labels and relationship types by default, you can run specific Cypher queries to create filtered views. For example, MATCH (caller:Function)-[r:CALLS]->(callee:Function) RETURN caller, callee, r displays only function call relationships, hiding imports and inheritance edges from the view.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →