npm install @neo4j/graphql@alpha graphql neo4j-driver @apollo/server
-
@neo4j/graphql
is the official Neo4j GraphQL Library package. It takes your GraphQL type definitions and generates a schema backed by a Neo4j database. -
graphql
generates a schema and execute queries and mutations. -
neo4j-driver
is the official Neo4j Driver package for JavaScript, of which an instance must be passed into the Neo4j GraphQL Library. -
The
@apollo/server
is the default GraphQL server package for Apollo Server.
The following JavaScript snippet connects to a Neo4j database.
Set your values for <neo4j-database-uri>
, <username>
and <password>
:
import neo4j from "neo4j-driver";
import { Neo4jGraphQL } from "@neo4j/graphql";
const driver = neo4j.driver(
"<neo4j-database-uri>",
neo4j.auth.basic("<username>", "<password>")
);
const neoSchema = new Neo4jGraphQL({ typeDefs, driver });
Extend your JavaScript with a constant that holds GraphQL type definitions. Here is a simple example with two node types, one with label "Actor" and the other "Movie":
const typeDefs = `#graphql
type Movie @node {
title: String
actors: [Actor!]! @relationship(type: "ACTED_IN", direction: IN)
}
type Actor @node {
name: String
movies: [Movie!]! @relationship(type: "ACTED_IN", direction: OUT)
}
`;
Extend your JavaScript to create an Apollo Server instance:
import { ApolloServer } from '@apollo/server';
import { startStandaloneServer } from '@apollo/server/standalone';
const server = new ApolloServer({
schema: await neoSchema.getSchema(),
});
const { url } = await startStandaloneServer(server, {
listen: { port: 4000 },
});
console.log(`🚀 Server ready at ${url}`);
You are ready to start up your GraphQL server.
Execute your JavaScript with node
.
If successful, you should see the following output:
🚀 Server ready at http://localhost:4000/
That is where the Apollo server starts.
Use the Apollo server to execute mutations and populate your database. See Create nodes in the database.