模拟和用户切换
这是 GraphQL 库版本 6 的文档。对于长期支持 (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 标头 User
和 Password
中提供,但这不建议用于生产环境
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}`);