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.
Steps
-
Install the Neo4j Go Driver in the integrated terminal window
shInstalling the neo4j-go-driver dependencygo get -u github.com/neo4j/neo4j-go-driver/v5
-
Import the
github.com/neo4j/neo4j-go-driver/v5/neo4j
symbol and use theneo4j
object to create a new instance of the Driver withUri
,Username
andPassword
credentials provided obtained using theGetNeo4jCredentials()
method -
Once you have created the Driver, open a new session and execute the following
cypher
statement using theparams
map.cypherFind the DirectorMATCH (p:Person)-[:DIRECTED]->(:Movie {title: $title}) RETURN p.name AS Director
-
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:
shRun The Challengego run pkg/challenges/create_driver/challenge.go
-
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:
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.
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[]
}
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.