> ## Documentation Index
> Fetch the complete documentation index at: https://codegeninc-fix-system-prompt-typo.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Neo4j Graph

<Frame caption="Function call graph for a codebase">
  <img src="https://mintcdn.com/codegeninc-fix-system-prompt-typo/bvq8QvFHwpTEhppB/images/neo4j-call-graph.png?fit=max&auto=format&n=bvq8QvFHwpTEhppB&q=85&s=2edc07e732a4aa5d692b7a9c3365746f" width="1415" height="1080" data-path="images/neo4j-call-graph.png" />
</Frame>

# Neo4j Graph

Codegen can export codebase graphs to Neo4j for visualization and analysis.

## Installation

In order to use Neo4j you will need to install it and run it locally using Docker.

### Neo4j

First, install Neo4j using the official [installation guide](https://neo4j.com/docs/desktop-manual/current/installation/download-installation/).

### Docker

To run Neo4j locally using Docker, follow the instructions [here](https://neo4j.com/docs/apoc/current/installation/#docker).

## Launch Neo4j Locally

```bash theme={null}
docker run \
    -p 7474:7474 -p 7687:7687 \
    -v $PWD/data:/data -v $PWD/plugins:/plugins \
    --name neo4j-apoc \
    -e NEO4J_apoc_export_file_enabled=true \
    -e NEO4J_apoc_import_file_enabled=true \
    -e NEO4J_apoc_import_file_use__neo4j__config=true \
    -e NEO4J_PLUGINS=\[\"apoc\"\] \
    neo4j:latest
```

## Usage

```python theme={null}
from codegen import Codebase
from codegen.extensions.graph.main import visualize_codebase

# parse codebase
codebase = Codebase("path/to/codebase")

# export to Neo4j
visualize_codebase(codebase, "bolt://localhost:7687", "neo4j", "password")
```

## Visualization

Once exported, you can open the Neo4j browser at `http://localhost:7474`, sign in with the username `neo4j` and the password `password`, and use the following Cypher queries to visualize the codebase:

### Class Hierarchy

```cypher theme={null}
Match (s: Class )-[r: INHERITS_FROM*]-> (e:Class) RETURN s, e LIMIT 10
```

<Frame caption="Class hierarchy for a codebase">
  <img src="https://mintcdn.com/codegeninc-fix-system-prompt-typo/bvq8QvFHwpTEhppB/images/neo4j-class-hierarchy.png?fit=max&auto=format&n=bvq8QvFHwpTEhppB&q=85&s=6af3f73a698d5dc1a4c219c160e29a88" width="1531" height="915" data-path="images/neo4j-class-hierarchy.png" />
</Frame>

### Methods Defined by Each Class

```cypher theme={null}
Match (s: Class )-[r: DEFINES]-> (e:Method) RETURN s, e LIMIT 10
```

<Frame caption="Methods defined by each class">
  <img src="https://mintcdn.com/codegeninc-fix-system-prompt-typo/bvq8QvFHwpTEhppB/images/neo4j-class-methods.png?fit=max&auto=format&n=bvq8QvFHwpTEhppB&q=85&s=74fe459be1a1893dd28b125f1d4d59cc" width="1015" height="941" data-path="images/neo4j-class-methods.png" />
</Frame>

### Function Calls

```cypher theme={null}
Match (s: Func )-[r: CALLS]-> (e:Func) RETURN s, e LIMIT 10
```

<Frame caption="Function call graph for a codebase">
  <img src="https://mintcdn.com/codegeninc-fix-system-prompt-typo/bvq8QvFHwpTEhppB/images/neo4j-function-calls.png?fit=max&auto=format&n=bvq8QvFHwpTEhppB&q=85&s=17a3460bfb1dfa85b217be3b696d8659" width="1084" height="760" data-path="images/neo4j-function-calls.png" />
</Frame>

### Call Graph

```cypher theme={null}
Match path = (:(Method|Func)) -[:CALLS*5..10]-> (:(Method|Func))
Return path 
LIMIT 20
```

<Frame caption="Call graph for a codebase">
  <img src="https://mintcdn.com/codegeninc-fix-system-prompt-typo/bvq8QvFHwpTEhppB/images/neo4j-call-graph.png?fit=max&auto=format&n=bvq8QvFHwpTEhppB&q=85&s=2edc07e732a4aa5d692b7a9c3365746f" width="1415" height="1080" data-path="images/neo4j-call-graph.png" />
</Frame>
