Getting started

The focus of this course has been using the Neo4j GraphQL Library via the Toolbox to develop, test, and query your GraphQL API.

However, it’s useful to understand how to install and get started with the Neo4j GraphQL Library.

The Neo4j GraphQL Library is a Node.js JavaScript library that can be used with JavaScript GraphQL implementations.

Use npm to install The Neo4j GraphQL Library as shown here:

bash
$ npm install @neo4j/graphql graphql neo4j-driver @apollo/server

This program uses the Neo4j GraphQL Library to generate a GraphQL schema and start a local GraphQL API.

JavaScript
index.mjs
import { ApolloServer } from '@apollo/server';
import { startStandaloneServer } from '@apollo/server/standalone';
import { Neo4jGraphQL } from "@neo4j/graphql";
import neo4j from "neo4j-driver";

const typeDefs = `#graphql
    type Movie {
        title: String
        actors: [Actor!]! @relationship(type: "ACTED_IN", direction: IN)
    }

    type Actor {
        name: String
        movies: [Movie!]! @relationship(type: "ACTED_IN", direction: OUT)
    }
`;

const driver = neo4j.driver(
    "bolt://localhost:7687",
    neo4j.auth.basic("neo4j", "letmein")
);

const neoSchema = new Neo4jGraphQL({ typeDefs, driver });

const server = new ApolloServer({
    schema: await neoSchema.getSchema(),
});

const { url } = await startStandaloneServer(server, {
    context: async ({ req }) => ({ req }),
    listen: { port: 4000 },
});

console.log(`🚀 Server ready at ${url}`);

Review the program and identify where:

  • The GraphQL type definition is created.

  • The connection to the Neo4j database is defined.

  • The schema is created using the Neo4j GraphQL library.

To run the program, you would need to update the connection details. Here are the connection details for your Neo4j Sandbox:

Bolt URI

bolt://52.3.230.46:7687

Username

neo4j

Password

operators-cruises-till

To start a local GraphQL API run:

bash
node index.mjs

The program will also serve the GraphQL Playground IDE at https://localhost:4000. You can use it to query the API and explore documentation using GraphQL’s introspection feature.

Screenshot of the GraphQL Playground IDE

The Neo4j GraphQL Library Getting Started guide is a great place to continue your learning of using GraphQL and Neo4j.

Check Your Understanding

1. Type Definitions Variable

In the GraphQL API program above what is the name of the variable which stores the GraphQL type definitions?

  • neoSchema

  • Neo4jGraphQL

  • typeDefs

Hint

The type definitions are passed to the Neo4jGraphQL constructor when creating the schema.

Solution

The type definitions are created at the start of the program and stored in the variable typeDefs.

2. GraphQL IDE

What is the name of the browser-based GraphQL IDE for executing GraphQL operations and exploring GraphQL API documentation?

Select the correct answer.

  • ✓ GraphQL Playground

  • ❏ Neo4j Browser

  • ❏ GraphQL Sunshine Beam

Hint

You can either run a Cypher statement within an auto-commit transaction or execute a Cypher statement within a managed transaction.

Solution

The answers are session.run() and session.executeRead().

Summary

In this lesson, you explored the features of the Neo4j GraphQL Library.

Chatbot

Hi, I am an Educational Learning Assistant for Intelligent Network Exploration. You can call me E.L.A.I.N.E.

How can I help you today?