# Getting relations for visualisation

**URL:** <https://community.neo4j.com/t/getting-relations-for-visualisation/28089>\
**Category:** Neo4j Visualization Library (NVL)\
**Tags:** cypher\
**Created:** [October 29, 2020, 1:15pm UTC](https://community.neo4j.com/t/getting-relations-for-visualisation/28089 "2020-10-29T13:15:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![drmartiner](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/drmartiner/32/12632_2.png) [@drmartiner](https://community.neo4j.com/u/drmartiner)\
**Post date:** [October 29, 2020, 1:15pm UTC](https://community.neo4j.com/t/getting-relations-for-visualisation/28089/1 "2020-10-29T13:15:16Z")

</div>

Hi everyone,

I want to draw a graph (nodes and relations). I try to get relations through python lib `neo4j`, but it doesn't work: I can't give relations (I have it, if I execute the query via Neo4j Browser). How can I get relations? What is better way to draw all graph looks like Neo4j Browser?

My simple code

```python
from neo4j import GraphDatabase, Result, Transaction, Session
from neo4j.graph import Graph

def print_graph(tx: Transaction) -> None:
    result: Result = tx.run("MATCH (a) Return *")
    graph: Graph = result.graph()

    print(f"nodes count is {len(graph.nodes)}") # It will be 3
    print(f"relationships count is {len(graph.relationships)}") # It will be 0

if __name__ == " __main__":
    driver = GraphDatabase.driver("neo4j://localhost:7687", auth=("neo4j", "changeme"))

    with driver.session() as session: # type: Session
        session.read_transaction(print_graph)

    driver.close()

```

 ![123](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/2X/9/9845a2218ea90c8da1279fa26a85494618980a1a.png)

---

<div class="post-metadata">

**Author:** ![accounts](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/accounts/32/4145_2.png) [@accounts](https://community.neo4j.com/u/accounts)\
**Post date:** [October 29, 2020, 8:54pm UTC](https://community.neo4j.com/t/getting-relations-for-visualisation/28089/2 "2020-10-29T20:54:08Z")

</div>

the browser makes 2 calls if i'm not mistaken  
the first is getting your data and then the second getting all relationships between all the nodes that came back by collecting all the node ids and then using them in the second call.

"[1,2,3]" below is the array of the node ids that came in the first request

```auto
match a-[r]-b where id(a) in [1,2...] and id(b) in [1,2,3...] return r

```

---

<div class="post-metadata">

**Author:** ![adam\_cowley](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/adam_cowley/32/145_2.png) [@adam\_cowley](https://community.neo4j.com/u/adam_cowley)\
**Post date:** [October 30, 2020, 9:23am UTC](https://community.neo4j.com/t/getting-relations-for-visualisation/28089/3 "2020-10-30T09:23:30Z")

</div>

The default setting in Neo4j Browser is to connect the nodes together. If you go to the settings pane (Settings cog on left hand side), then scroll down to Graph Visualization, untick "Connect result nodes" and re-run the result you will just see the nodes without relationships between them.

In order to get a count of the relationships you will also have to return them in the query.

```auto
MATCH (start)-[r]->(end)
RETURN start, r, end

```

However, returning _everything_ from a larger graph may take a long time. Instead you could do something like...

```auto
MATCH (n) 
 // Limit the result as Neo4j Browser would do
WITH n LIMIT 25

// Get the neighbouring nodes
MATCH (n)-[r]->(other) 

// Aggregate by relationship type
WITH n, type(r) AS relationship, collect(other) AS nodes. 

// Aggregate by node
RETURN n, collect({ relationship: relationship, nodes: nodes}) AS relationships

```

This will give you one row for each of those initial 25 nodes, then an array of relationship types and the collected nodes, along the lines of:

```auto
[
  { relationship: 'ACTED_IN', nodes: [...]},
  { relationship: 'DIRECTED', nodes: [...]}
]

```

Limit your results to certain labels and relationship types where possible ;)

---

<div class="post-metadata">

**Author:** ![drmartiner](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/drmartiner/32/12632_2.png) [@drmartiner](https://community.neo4j.com/u/drmartiner)\
**Post date:** [October 30, 2020, 9:55am UTC](https://community.neo4j.com/t/getting-relations-for-visualisation/28089/4 "2020-10-30T09:55:28Z")

</div>

> [@adam\_cowley](#):
>
> ```auto
> MATCH (start)-[r]->(end)
> RETURN start, r, end
> 
> ```

Great! Many thanks for the solution!
