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 Go 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

Returning graph types

When graph types are returned by a query executed in the Query window, they are visualized in a graph layout.

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

nil

null

bool

Boolean

int64

Integer

float64

Float

string

String

[]byte

Bytes [1]

[]any

List

map[string]any

Map

Graph types

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

go
Return Nodes and Relationships
result, err := neo4j.ExecuteQuery(ctx, driver, `
MATCH path = (person:Person)-[actedIn:ACTED_IN]->(movie:Movie {title: $title})
RETURN path, person, actedIn, movie
`, map[string]any{"title": "Toy Story"}, neo4j.EagerResultTransformer)

Nodes

Nodes are returned as a neo4j.Node object.

go
for _, record := range result.Records {
    node, _ := record.Get("movie")
    movieNode := node.(neo4j.Node)
}
go
Working with Node Objects
fmt.Println(movieNode.ElementId)             // (1)
fmt.Println(movieNode.Labels)                // (2)
fmt.Println(movieNode.Props)                 // (3)

if name, ok := movieNode.Props["name"]; ok { // (4)
    fmt.Println("name:", name)
}
  1. The ElementId property provides access to the node’s element ID
    eg. 4:97b72e9c-ae4d-427c-96ff-8858ecf16f88:0

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

  3. The Props property provides access to the node’s properties as a map.
    eg. {name: "Tom Hanks", tmdbId: "31"}

  4. A single property can be retrieved using the Props map.

Relationships

Relationships are returned as a neo4j.Relationship object.

go
actedIn, _ := record.Get("actedIn")
relationship := actedIn.(neo4j.Relationship)

fmt.Println(relationship.ElementId)             // (1)
fmt.Println(relationship.Type)                  // (2)
fmt.Println(relationship.Props)                 // (3)

if role, ok := relationship.Props["role"]; ok { // (4)
    fmt.Println("role:", role)
}
  1. ElementId - Internal ID of the relationship (eg. 9876)

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

  3. Props - Returns relationship properties as a map (eg. {role: "Woody"})

  4. Individual properties can be accessed using the Props map.

Paths

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

go
path, _ := record.Get("path")
pathObj := path.(neo4j.Path)

fmt.Println(len(pathObj.Nodes))         // (1)
fmt.Println(len(pathObj.Relationships)) // (2)

// Iterate over nodes in the path
for i, node := range pathObj.Nodes {     // (3)
    fmt.Printf("Node %d: %v\n", i, node.Labels)
}

// Iterate over relationships in the path
for i, rel := range pathObj.Relationships { // (4)
    fmt.Printf("Relationship %d: %s\n", i, rel.Type)
}
  1. len(pathObj.Nodes) - The number of nodes in the path

  2. len(pathObj.Relationships) - The number of relationships within the path

  3. pathObj.Nodes - A slice of neo4j.Node objects that can be iterated over

  4. pathObj.Relationships - A slice of neo4j.Relationship objects that can be iterated over

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.

Chatbot

How can I help you today?