In this challenge you will:
-
Connect to a Neo4j database
-
Open a new session
-
Run a Cypher statement to find the director of Toy Story.
You must set up a development environment to run the code.
Get started
The repository, neo4j-graphacademy/app-typescript, has been created for this course. It contains any starter code and resources you need.
You can use a GitHub codespace as an online IDE and workspace for this course. It will automatically clone the course repository and set up your environment.
GitHub Codespaces
You will need to login with a GitHub account. The GitHub Codespaces free monthly usage will cover the duration of this course.
Develop on your local machine
You will need NodeJS installed and the ability to install packages using npm
.
Clone the github.com/neo4j-graphacademy/app-typescript repository:
git clone https://github.com/neo4j-graphacademy/app-typescript
Install the required packages using npm
:
npm install
npm i typescript ts-node
You do not need to create a Neo4j database as you will use the provided sandbox instance.
Setup the environment
Create a copy of the .env.example
file and name it .env
.
Fill in the required values.
NEO4J_URI="bolt://..."
NEO4J_USERNAME="neo4j"
NEO4J_PASSWORD="..."
Update the Neo4j sandbox connection details:
- NEO4J_URI
-
bolt://{sandbox-ip}:{sandbox-boltPort}
- NEO4J_USERNAME
-
{sandbox-username}
- NEO4J_PASSWORD
-
{sandbox-password}
Steps
Open the src/challenges/create-driver/challenge.ts
file that contains the starter code.
import dotenv from 'dotenv';
dotenv.config({ path: '.env' });
// Import the driver
import neo4j, { Integer, Node, Relationship } from 'neo4j-driver'
import { getNeo4jCredentials } from '../utils'
// Neo4j Credentials
const {
NEO4J_URI,
NEO4J_USERNAME,
NEO4J_PASSWORD
} = getNeo4jCredentials()
async function main() {
// TODO: Create a Driver Instance using neo4j.driver()
// TODO: Open a new Session using driver.session()
try {
const cypher = `
MATCH (p:Person)-[:DIRECTED]->(:Movie {title: $title})
RETURN p.name AS Director
`
const params = { title: 'Toy Story' }
// TODO: Run the Cypher statement using session.run()
// TODO: Log the Director value of the first record
}
finally {
// TODO: Close the session
}
}
main()
-
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.
typescriptconst driver = neo4j.driver( NEO4J_URI, neo4j.auth.basic(NEO4J_USERNAME, NEO4J_PASSWORD) )
-
-
Open a new session:
typescriptconst session = driver.session()
-
Use the session to run the Cypher query to find the director of Toy Story:
typescriptconst cypher = ` MATCH (p:Person)-[:DIRECTED]->(:Movie {title: $title}) RETURN p.name AS Director ` const params = { title: 'Toy Story' } // Run the Cypher statement using session.run() const res = await session.run(cypher, params)
-
Log the result to the console:
typescriptconsole.log(res.records[0].get('Director'))
-
Finally, close the session:
await session.close()
View the complete code
import dotenv from 'dotenv';
dotenv.config({ path: '.env' });
// Import the driver
import neo4j, { Integer, Node, Relationship } from 'neo4j-driver'
import { getNeo4jCredentials } from '../utils'
// Neo4j Credentials
const {
NEO4J_URI,
NEO4J_USERNAME,
NEO4J_PASSWORD
} = getNeo4jCredentials()
async function main() {
// 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 {
const cypher = `
MATCH (p:Person)-[:DIRECTED]->(:Movie {title: $title})
RETURN p.name AS Director
`
const params = { title: 'Toy Story' }
// Run the Cypher statement using session.run()
const res = await session.run(cypher, params)
// Log the Director value of the first record
console.log(res.records[0].get('Director'))
}
finally {
// Close the session
await session.close()
}
}
main()
Run the Challenge
Run the file using ts-node
to view the result:
ts-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.
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
import dotenv from 'dotenv';
dotenv.config({ path: '.env' });
// Import the driver
import neo4j, { Integer, Node, Relationship } from 'neo4j-driver'
import { getNeo4jCredentials } from '../utils'
// Neo4j Credentials
const {
NEO4J_URI,
NEO4J_USERNAME,
NEO4J_PASSWORD
} = getNeo4jCredentials()
async function main() {
// 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 {
const cypher = `
MATCH (p:Person)-[:DIRECTED]->(:Movie {title: $title})
RETURN p.name AS Director
`
const params = { title: 'Toy Story' }
// Run the Cypher statement using session.run()
const res = await session.run(cypher, params)
// Log the Director value of the first record
console.log(res.records[0].get('Director'))
}
finally {
// Close the session
await session.close()
}
}
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.