Writing Data to Neo4j

Your challenge is to modify another pre-written file to add yourself as an actor in The Matrix.

Open Challenge in an Online IDE →

Steps

  1. Update the params object to use your name. This step isn’t strictly required, just a bit of fun.

  2. The Cypher statement is already written for you. Use the neo4j.ExecuteRead() method, passing the ctx and session variables, and a callback function to represent the unit of transactional work.

  3. In that function you must call the Run() method on the first parameter passed to the query, using the cypher and params variables.

  4. await the results and use fmt.Println() to check that the code has executed correctly.

  5. To add the new node and relationship to the database, click the Debug icon to the left of the window and run Writing Data Challenge task, or use the integrated terminal window to run the following command:

    sh
    Run The Challenge
    go run pkg/challenges/write/challenge.go
  6. Once the code has run, click Verify and we will check that the node has been added to the database.

Verifying the Test

Once you have executed the code, click the Verify button and we will check that the code has been executed successfully.

Hint

To pass this challenge you must run the Cypher statement in a write transaction using the neo4j.ExecuteWrite() or session.ExecuteWrite() methods.

Solution

Compare your code with the solution here

go
package main

// Import the driver
import (
	"context"
	"fmt"

	. "github.com/neo4j-graphacademy/neoflix/pkg/shared"
	"github.com/neo4j/neo4j-go-driver/v5/neo4j"
)

func main() {
	// Neo4j Credentials
	credentials := GetNeo4jCredentials()
	// Create a Driver Instance
	ctx := context.Background()
	driver, err := neo4j.NewDriverWithContext(
		credentials.Uri,
		neo4j.BasicAuth(credentials.Username, credentials.Password, ""),
	)
	PanicOnErr(err)
	defer PanicOnClosureError(ctx, driver)

	// Open a new Session
	session := driver.NewSession(ctx, neo4j.SessionConfig{})
	defer PanicOnClosureError(ctx, session)

	// tag::solution[]
	// Execute the `cypher` statement in a write transaction
	cypher := `
		MATCH (m:Movie {title: $title})
		CREATE (p:Person {name: $name})
		CREATE (p)-[:ACTED_IN]->(m)
		RETURN p`
	params := map[string]any{
		"title": "Matrix, The",
		"name":  "Your Name",
	}

	// execute a transaction function with neo4j.ExecuteWrite[T]
	// and properly get the result as a neo4j.Node
	personNode, err := neo4j.ExecuteWrite[neo4j.Node](ctx, session,
		func(tx neo4j.ManagedTransaction) (neo4j.Node, error) {
			result, err := tx.Run(ctx, cypher, params)
			if err != nil {
				return *new(neo4j.Node), err
			}
			// same as before: extract the single result
			// and return it as a neo4j.Node
			return neo4j.SingleTWithContext(ctx, result,
				func(record *neo4j.Record) (neo4j.Node, error) {
					node, _, err := neo4j.GetRecordValue[neo4j.Node](record, "p")
					return node, err
				})
		})
	PanicOnErr(err)
	fmt.Println(personNode)
	// end::solution[]
}

Click here to view the file on Github

Lesson Summary

In this challenge, you used your knowledge to create a driver instance and execute a Cypher statement.

You should now have everything you need to use the Neo4j Go Driver in your next Go project.