Skip to content

Latest commit

 

History

History
93 lines (65 loc) · 2.64 KB

connect-drivers-graphql.adoc

File metadata and controls

93 lines (65 loc) · 2.64 KB

Install the Neo4j GraphQL Library and dependencies

npm install @neo4j/graphql@alpha graphql neo4j-driver @apollo/server
  1. @neo4j/graphql is the official Neo4j GraphQL Library package. It takes your GraphQL type definitions and generates a schema backed by a Neo4j database.

  2. graphql generates a schema and execute queries and mutations.

  3. neo4j-driver is the official Neo4j Driver package for JavaScript, of which an instance must be passed into the Neo4j GraphQL Library.

  4. The @apollo/server is the default GraphQL server package for Apollo Server.

Connect to the database

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 });

Set GraphQL type definitions

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)
    }
`;

Create an instance of ApolloServer

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}`);

Start the server

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.