模拟用户和用户切换

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

模拟用户和用户切换是 Neo4j 数据库和驱动程序的功能,允许在与初始连接不同的上下文中执行查询。

模拟用户

模拟用户仍使用原始配置的用户身份验证到数据库,但在模拟用户的上下文中运行查询。当模拟用户时,查询在模拟用户的完整安全上下文中运行,而不是已验证用户的安全上下文(包括主数据库、权限等)。

以下是每请求模拟不同用户的示例。此处要模拟的用户取自 HTTP 标头 User

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

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

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

const schema = await neo4jGraphql.getSchema();

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

const { url } = await startStandaloneServer(server, {
  // Your async context function should async and return an object
  context: async ({ req }) => ({
    sessionConfig: {
        impersonatedUser: req.headers.user,
    },
  }),
});

console.log(`🚀  Server ready at: ${url}`);
import { ApolloServer } from "@apollo/server";
import { startStandaloneServer } from "@apollo/server/standalone";
import { Neo4jGraphQL, Neo4jGraphQLContext } from "@neo4j/graphql";
import neo4j from "neo4j-driver";

const typeDefs = `#graphql
    type Movie @node {
        title: String!
    }
`;

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

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

const schema = await neo4jGraphql.getSchema();

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

const { url } = await startStandaloneServer(server, {
  // Your async context function should async and return an object
  context: async ({ req }) => ({
    sessionConfig: {
        impersonatedUser: req.headers.user,
    },
  }),
});

console.log(`🚀  Server ready at: ${url}`);

用户切换

用户切换会为给定会话完全切换向数据库进行身份验证的用户,而无需承担实例化全新驱动程序实例的性能开销。

在以下示例中可以找到按请求配置用户切换的示例。请注意,用户名和密码在 HTTP 标头 UserPassword 中提供,但这不建议用于生产环境

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

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

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

const schema = await neo4jGraphql.getSchema();

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

const { url } = await startStandaloneServer(server, {
  // Your async context function should async and return an object
  context: async ({ req }) => ({
    sessionConfig: {
        auth: neo4j.auth.basic(req.headers.user, req.headers.password),
    },
  }),
});

console.log(`🚀  Server ready at: ${url}`);
import { ApolloServer } from "@apollo/server";
import { startStandaloneServer } from "@apollo/server/standalone";
import { Neo4jGraphQL, Neo4jGraphQLContext } from "@neo4j/graphql";
import neo4j from "neo4j-driver";

const typeDefs = `#graphql
    type Movie @node {
        title: String!
    }
`;

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

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

const schema = await neo4jGraphql.getSchema();

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

const { url } = await startStandaloneServer(server, {
  // Your async context function should async and return an object
  context: async ({ req }) => ({
    sessionConfig: {
        auth: neo4j.auth.basic(req.headers.user, req.headers.password),
    },
  }),
});

console.log(`🚀  Server ready at: ${url}`);
© . All rights reserved.