Driver Configuration

Neo4j Driver

An instance of the Neo4j JavaScript driver must be present in either the GraphQL request context, or construction of your Neo4jGraphQL instance (or alternatively, OGM).

The examples in this chapter 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

const { Neo4jGraphQL } = require("@neo4j/graphql");
const neo4j = require("neo4j-driver");

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

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

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

const server = new ApolloServer({
    schema: neoSchema.schema,
    context: ({ req }) => ({ req }),
});

Driver in context

const { Neo4jGraphQL } = require("@neo4j/graphql");
const neo4j = require("neo4j-driver");

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

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

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

const server = new ApolloServer({
    schema: neoSchema.schema,
    context: ({ req }) => ({ req, driver }),
});

OGM

const { OGM } = require("@neo4j/graphql-ogm");
const neo4j = require("neo4j-driver");

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

const driver = neo4j.driver(
    "bolt://localhost:7687",
    neo4j.auth.basic("neo4j", "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 will throw an Error if the DBMS is incompatible, with details of the incompatibilities.

Neo4jGraphQL

const { Neo4jGraphQL } = require("@neo4j/graphql");
const neo4j = require("neo4j-driver");

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

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

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

OGM

const { OGM } = require("@neo4j/graphql-ogm");
const neo4j = require("neo4j-driver");

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

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

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

Specifying Neo4j database

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

Context

const { Neo4jGraphQL } = require("@neo4j/graphql");
const neo4j = require("neo4j-driver");

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

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

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

const server = new ApolloServer({
  schema,
  context: { driverConfig: { database: "my-database" } }
});

Neo4jGraphQL constructor

const { Neo4jGraphQL } = require("@neo4j/graphql");
const neo4j = require("neo4j-driver");

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

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

const neoSchema = new Neo4jGraphQL({
    typeDefs,
    driver,
    config: {
        driverConfig: {
            database: "my-database",
        },
    },
});

const server = new ApolloServer({
  schema,
});

Specifying Neo4j Bookmarks

You might have a requirement to specify Neo4j bookmarks when executing a query against your GraphQL schema. Primarily you will need to do this to chain transactions to ensure causal consistency if using a causal cluster or Aura Professional.

You can read more about causal consistency in the clustering chapter of the Neo4j Operations manual, and more about bookmark chaining in the driver manual.

You can ask for the bookmark in the selection set from the info object of a Mutation response. For example, for a type User:

mutation($name: String!) {
    createUsers(input: [{ name: $name }]) {
        info {
            bookmark
        }
        users {
            name
        }
    }
}

You can then pass this bookmark into the context of a subsequent query, like this setup to pass in via a HTTP request header for example:

const { Neo4jGraphQL } = require("@neo4j/graphql");
const neo4j = require("neo4j-driver");

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

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

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

const server = new ApolloServer({
  schema,
  context: (req) => ({ driverConfig: { bookmarks: [req.headers["x-neo4j-bookmark"]] } })
});