驱动程序配置
此页面描述了 Neo4j GraphQL 库驱动程序的配置。
Neo4j 驱动程序
要使 Neo4j GraphQL 库正常工作,必须在 Neo4jGraphQL
实例(或 OGM
)的构造过程中传入 Neo4j JavaScript 驱动程序
的实例,或者在每次请求时将驱动程序、会话或事务传入 context.executionContext
。
此页面中的示例假设 Neo4j 数据库在“bolt://localhost:7687”上运行,用户名为“username”,密码为“password”。
Neo4j GraphQL 库
Neo4jGraphQL
构造函数中的驱动程序
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 }),
});
上下文中的驱动程序
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 }),
});
上下文中的会话
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 }),
});
上下文中的事务
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 }),
});
数据库兼容性
使用 Neo4jGraphQL
或 OGM
实例上可用的 checkNeo4jCompat
方法来确保指定的 DBMS 具有所需的版本,并且具有必要的可用函数和过程。如果 DBMS 不兼容,checkNeo4jCompat
将抛出 Error
,其中包含不兼容性的详细信息。
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();
指定 Neo4j 数据库
通过 context
中的 sessionConfig
下的 database
字段指定要在 DBMS 中使用的数据库,所有解析器都共享该 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" }}),
});