驱动程序配置

这是 GraphQL Library 7 版本的文档。对于长期支持 (LTS) 版本 5,请参考 GraphQL Library 5 LTS 版本

本页描述 Neo4j GraphQL 库驱动的配置。

Neo4j 驱动

要使 Neo4j GraphQL 库正常工作,需要在创建 Neo4jGraphQL 实例时传入 Neo4j JavaScript 驱动 的实例,或者在每个请求的 context.executionContext 中传入驱动、会话或事务。

本页的示例假设 Neo4j 数据库运行在 "bolt://: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 @node {
        name: String
    }
`;

const driver = neo4j.driver(
    "bolt://: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 @node {
        name: String
    }
`;

const driver = neo4j.driver(
    "bolt://: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 @node {
        name: String
    }
`;

const driver = neo4j.driver(
    "bolt://: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 @node {
        name: String
    }
`;

const driver = neo4j.driver(
    "bolt://:7687",
    neo4j.auth.basic("username", "password")
);
const session = driver.session();
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 }),
});

上下文中的事务配置

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

const driver = neo4j.driver(
    "bolt://:7687",
    neo4j.auth.basic("username", "password")
);
const session = driver.session();
const transactionConfig = {
    timeout: 60 * 1000,
    metadata: {
        "my-very-own-metadata": "is very good!"
    }
};

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

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

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

数据库兼容性

使用 Neo4jGraphQL 实例上的 checkNeo4jCompat 方法,可确保指定的 DBMS 版本符合要求,并且具备必要的函数和过程。如果 DBMS 不兼容,checkNeo4jCompat 将抛出 Error,并提供不兼容细节。

Neo4jGraphQL

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

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

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

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

指定 Neo4j 数据库

通过在所有解析器共享的 context 中的 sessionConfig 下的 database 字段,指定要在 DBMS 中使用的数据库。

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

const driver = neo4j.driver(
    "bolt://: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" }}),
});