Driver Configuration
Neo4j Driver
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 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 });
neoSchema.getSchema().then((schema) => {
const server = new ApolloServer({
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 });
neoSchema.getSchema().then((schema) => {
const server = new ApolloServer({
schema,
context: ({ req }) => ({ req, executionContext: 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 });
neoSchema.getSchema().then((schema) => {
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",
},
},
});
neoSchema.getSchema().then((schema) => {
const server = new ApolloServer({
schema,
});
});
Specifying Neo4j version
When a connection is established, the library automatically detect the version of the Neo4j instance connected. The version will be then stored and used for the following queries. It is also possible to specify manually the Neo4j version in the Context.
Context
const { Neo4jGraphQL, Neo4jDatabaseInfo } = 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 });
neoSchema.getSchema().then((schema) => {
const server = new ApolloServer({
schema,
context: { neo4jDatabaseInfo: new Neo4jDatabaseInfo("4.4") },
});
});
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 });
neoSchema.getSchema().then((schema) => {
const server = new ApolloServer({
schema,
context: ({ req }) => ({ driverConfig: { bookmarks: [req.headers["x-neo4j-bookmark"]] } }),
});
});
Was this page helpful?