Creating a Driver Instance

Your challenge is to add a new driver instance to an existing file with the connection details provided. Once you have created the driver, you will need to open a new session and execute a Cypher statement to find the director of the movie Toy Story.

Open Challenge in an Online IDE →

Steps

  1. Install the Neo4j Go Driver in the integrated terminal window

    sh
    Installing the neo4j-go-driver dependency
    go get -u github.com/neo4j/neo4j-go-driver/v5
  2. Import the github.com/neo4j/neo4j-go-driver/v5/neo4j symbol and use the neo4j object to create a new instance of the Driver with Uri, Username and Password credentials provided obtained using the GetNeo4jCredentials() method

  3. Once you have created the Driver, open a new session and execute the following cypher statement using the params map.

    cypher
    Find the Director
    MATCH (p:Person)-[:DIRECTED]->(:Movie {title: $title})
    RETURN p.name AS Director
  4. To find the answer, click the Debug icon to the left of the IDE window and run Create Driver Challenge task, or use the integrated terminal window to run the following command:

    sh
    Run The Challenge
    go run pkg/challenges/create_driver/challenge.go
  5. Once you have the result, copy and paste it into the text box below and click Check Answer.

Your Answer

Who directed Toy Story?

Take the Director value output from the statement above and paste it into the box below.

  • ✓ John Lasseter

Hint

You need to call the neo4j.NewDriverWithContext() object to create a Driver instance using the Uri, Username and Password variables provided by the GetNeo4jCredentials() function, then open a new session, execute the Cypher statement and log the Director value of the first record.

Once you have a result object, use the neo4j.SingleTWithContext() function to extract the Director value from the first record.

Your fmt.Println() call should look similar to the code block below:

ts
fmt.Println(director)

Copy the answer without any quotes or whitespace.

Solution

John Lasseter directed Toy Story.

You can compare your code with the solution here.

go
package main

// tag::import[]
// Import the driver
import (
	"context"
	"fmt"

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

// end::import[]

func main() {
	// tag::credentials[]
	// Neo4j Credentials
	credentials := GetNeo4jCredentials()
	// end::credentials[]

	// Cypher Query and Parameters
	cypher := `
      MATCH (p:Person)-[:DIRECTED]->(:Movie {title: $title})
      RETURN p.name AS Director
    `
	params := map[string]any{"title": "Toy Story"}

	// tag::solution[]
	// 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)

	// Run a Cypher statement
	result, err := session.Run(ctx, cypher, params)
	PanicOnErr(err)

	// Log the Director value of the first record
	director, err := neo4j.SingleTWithContext[string](ctx, result,
		// Extract the single record and transform it with a function
		func(record *neo4j.Record) (string, error) {
			// Extract the record value by the specified key
			// and map it to the specified generic type constraint
			director, _, err := neo4j.GetRecordValue[string](record, "Director")
			return director, err
		})
	PanicOnErr(err)
	fmt.Println(director)
	// end::solution[]
}

Click here to view the solution.

Lesson Summary

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

In the next lesson, you will learn about the different transaction functions and when to use them.