Graph types

Introduction

Let’s take a look at the types of data returned by a Cypher query.

The majority of the types returned by a Cypher query are mapped directly to Python types, but some more complex types need special handling.

  • Graph types - Nodes, Relationships and Paths

  • Temporal types - Dates and times

  • Spatial types - Points and distances

Types in Neo4j Browser

When graph types are returned by a query, they are visualized in a graph layout.

Learn how to interact with Neo4j from Python using the Neo4j Python DriverDirect mapping
Python Type Neo4j Cypher Type

None

null

bool

Boolean

int

Integer

float

Float

str

String

bytearray

Bytes [1]

list

List

dict

Map

Graph types

The following code snippet finds all movies with the specified title and returns person, acted_in and movie.

python
Return Nodes and Relationships
records, summary, keys = driver.execute_query("""
MATCH path = (person:Person)-[actedIn:ACTED_IN]->(movie:Movie {title: $title})
RETURN path, person, actedIn, movie
""", title=movie)

Nodes

Nodes are returned as a Node object.

python
for record in records:
    node = record["movie"]
python
Working with Node Objects
print(node.id)              # (1)
print(node.labels)          # (2)
print(node.items())         # (3)

# (4)
print(node["name"])
print(node.get("name", "N/A"))
  1. The element_id property provides access to the node’s element ID
    eg. 4:97b72e9c-ae4d-427c-96ff-8858ecf16f88:0

  2. The labels property is a frozenset containing an array of labels attributed to the Node
    eg. ['Person', 'Actor']

  3. The items() method provides access to the node’s properties as an iterable of all name-value pairs.
    eg. {name: 'Tom Hanks', tmdbId: '31' }

  4. A single property can be retrieved by either using [] brackets or using the get() method. The get() method also allows you to define a default property if none exists.

Relationships

Relationships are returned as a Relationship object.

python
acted_in = record["actedIn"]

print(acted_in.id)         # (1)
print(acted_in.type)       # (2)
print(acted_in.items())    # (3)

# 4
print(acted_in["roles"])
print(acted_in.get("roles", "(Unknown)"))

print(acted_in.start_node) # (5)
print(acted_in.end_node)   # (6)
  1. id - Internal ID of the relationship (eg. 9876)

  2. type - Type of relationship (eg. ACTED_IN)

  3. items() - Returns relationship properties as name-value pairs (eg. {role: 'Woody'})

  4. Access properties using brackets [] or get() method

  5. start_node - ID of the starting node

  6. end_node - ID of the ending node

Paths

A path is a sequence of nodes and relationships and is returned as a Path object.

python
path = record["path"]

print(path.start_node)  # (1)
print(path.end_node)    # (2)
print(len(path))  # (1)
print(path.relationships)  # (1)
  1. start_node - a Neo4j Integer representing the internal ID for the node at the start of the path

  2. end_node - a Neo4j Integer representing the internal ID for the node at the end of the path

  3. len(path) - A count of the number of relationships within the path

  4. relationships - An array of Relationship objects within the path.

Paths are iterable

Use iter(path) to iterate over the relationships in a path.

Advance to challenge

Summary

In this lesson, you learned about the types of data returned by a Cypher query and how to work with them in your application.

Now it’s time to test yourself on what you’ve learned.