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 must open a new session and run a Cypher statement to find the director of Toy Story.
Steps
-
Install the Neo4j JavaScript Driver in the integrated terminal window
shInstalling the neo4j-driver dependencynpm install neo4j-driver
-
Use the
neo4j
object to create a new instance of the Driver with the credentials provided:-
NEO4J_URI
is the connection string -
NEO4J_USERNAME
andNEO4J_PASSWORD
should be used to create the authentication token.
-
-
Once you have created the Driver, open a new session and run the following query:
cypherFind the DirectorMATCH (p:Person)-[:DIRECTED]->(:Movie {title: "Toy Story"}) RETURN p.name AS Director
The statement requires the following parameters:
typescript{ title: 'Toy Story' }
-
To find the answer, click the Debug icon to the left of the window and run Create Driver Challenge task, or use the integrated terminal window to run the following command:
shRun The Challengets-node src/challenges/create-driver/challenge.ts
-
Once you have the result, copy and paste it into the text box below and click Check Answer.
View the Solution
// tag::import[]
// Import the driver
import neo4j, { Integer, Node, Relationship } from 'neo4j-driver'
import { getNeo4jCredentials } from '../utils'
// end::import[]
// tag::credentials[]
// Neo4j Credentials
const {
NEO4J_URI,
NEO4J_USERNAME,
NEO4J_PASSWORD
} = getNeo4jCredentials()
// end::credentials[]
async function main() {
// tag::solution[]
// Create a Driver Instance using neo4j.driver()
const driver = neo4j.driver(
NEO4J_URI,
neo4j.auth.basic(NEO4J_USERNAME, NEO4J_PASSWORD)
)
// Open a new Session using driver.session()
const session = driver.session()
try {
// Run this Cypher statement using session.run()
const cypher = `
MATCH (p:Person)-[:DIRECTED]->(:Movie {title: $title})
RETURN p.name AS Director
`
const params = { title: 'Toy Story' }
const res = await session.run(cypher, params)
// Log the Director value of the first record
console.log(res.records[0].get('Director'))
}
finally {
await session.close()
}
// end::solution[]
}
main()
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.driver()
object to create a Driver instance using the NEO4J_URI
, NEO4J_USERNAME
and NEO4J_PASSWORD
variables, then open a new session, execute the Cypher statement and log the Director
value of the first record.
Your console.log()
statement should look similar to the code block below:
console.log(res.records[0].get('Director'))
Copy the answer without any quotes or whitespace.
Solution
John Lasseter directed Toy Story.
Compare your code with the solution here
// tag::import[]
// Import the driver
import neo4j, { Integer, Node, Relationship } from 'neo4j-driver'
import { getNeo4jCredentials } from '../utils'
// end::import[]
// tag::credentials[]
// Neo4j Credentials
const {
NEO4J_URI,
NEO4J_USERNAME,
NEO4J_PASSWORD
} = getNeo4jCredentials()
// end::credentials[]
async function main() {
// tag::solution[]
// Create a Driver Instance using neo4j.driver()
const driver = neo4j.driver(
NEO4J_URI,
neo4j.auth.basic(NEO4J_USERNAME, NEO4J_PASSWORD)
)
// Open a new Session using driver.session()
const session = driver.session()
try {
// Run this Cypher statement using session.run()
const cypher = `
MATCH (p:Person)-[:DIRECTED]->(:Movie {title: $title})
RETURN p.name AS Director
`
const params = { title: 'Toy Story' }
const res = await session.run(cypher, params)
// Log the Director value of the first record
console.log(res.records[0].get('Director'))
}
finally {
await session.close()
}
// end::solution[]
}
main()
Lesson Summary
In this challenge, you used your knowledge to create a driver instance and run a Cypher statement.
In the next lesson, you will learn about the different transaction functions and when to use them.