Driver configuration

This page describes the configuration of the Neo4j GraphQL Library driver.

Neo4j Driver

For the Neo4j GraphQL Library to work, either an instance of the Neo4j JavaScript driver must be passed in on construction of your Neo4jGraphQL instance (or alternatively, OGM), or a driver, session or transaction passed into the context.executionContext per request.

The examples in this page assume a Neo4j database running at "bolt://localhost:7687" with a username of "neo4j" and a password of "password".

Neo4j GraphQL Library

Driver in Neo4jGraphQL constructor

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 User {
        name: String
    }
`;

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

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

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

await startStandaloneServer(server, {
    context: async ({ req }) => ({ req }),
});

Driver in context

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 User {
        name: String
    }
`;

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

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

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

await startStandaloneServer(server, {
    context: async ({ req }) => ({ req, executionContext: driver }),
});

Session in context

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 User {
        name: String
    }
`;

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

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

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

await startStandaloneServer(server, {
    context: async ({ req }) => ({ req, executionContext: session }),
});

Transaction in context

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 User {
        name: String
    }
`;

const driver = neo4j.driver(
    "bolt://localhost:7687",
    neo4j.auth.basic("username", "password")
);
const session = driver.transaction();
const transaction = session.beginTransaction();

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

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

await startStandaloneServer(server, {
    context: async ({ req }) => ({ req, executionContext: transaction }),
});

OGM

import { OGM } from "@neo4j/graphql-ogm";
import neo4j from "neo4j-driver";

const typeDefs = `#graphql
    type User {
        name: String
    }
`;

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

const ogm = new OGM({ typeDefs, driver });

Database compatibility

Use the checkNeo4jCompat method available on either a Neo4jGraphQL or OGM instance to ensure the specified DBMS is of the required version, and has the necessary functions and procedures available. The checkNeo4jCompat throws an Error if the DBMS is incompatible, with details of the incompatibilities.

Neo4jGraphQL

import { Neo4jGraphQL } from "@neo4j/graphql";
import neo4j from "neo4j-driver";

const typeDefs = `#graphql
    type User {
        name: String
    }
`;

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

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

OGM

import { OGM } from "@neo4j/graphql-ogm";
import neo4j from "neo4j-driver";

const typeDefs = `#graphql
    type User {
        name: String
    }
`;

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

const ogm = new OGM({ typeDefs, driver });
await ogm.checkNeo4jCompat();

Specifying the Neo4j database

There are two ways to specify which database within a DBMS should be used.

Context

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 User {
        name: String
    }
`;

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

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

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

await startStandaloneServer(server, {
    context: async ({ req }) => ({ req, sessionConfig: { database: "my-database" }}),
});