@graphql-authz/ apollo-server-plugin (英语)
配置阿波罗服务器
确保配置上下文解析器以进行认证,并在上下文中添加用户信息
const server = new ApolloServer({
...
context: ({ req }) => {
return {
user: someHowAuthenticateUser(req.get("authorization"))),
};
},
...
});
用于指令的使用
应用 authZ Directive Transfer 到 schema 并添加插件到 apollo 服务器
import { makeExecutableSchema } from '@graphql-tools/schema';
import { authZDirective } from '@graphql-authz/directive';
import { authZApolloPlugin } from "@graphql-authz/apollo-server-plugin";
const { authZDirectiveTransformer } = authZDirective();
const schema = makeExecutableSchema({
typeDefs,
resolvers
});
const transformedSchema = authZDirectiveTransformer(schema);
const server = new ApolloServer({
...
schema: transformedSchema,
plugins: [authZApolloPlugin({ rules })],
...
});
用于扩展用途
向 apollo 服务器添加插件
import { authZApolloPlugin } from "@graphql-authz/apollo-server-plugin";;
const server = new ApolloServer({
...
plugins: [authZApolloPlugin({ rules })],
...
});
用于 AuthSchema 用途
在 apollo 服务器上添加插件并附加一个参数 authSchema
import { authZApolloPlugin } from "@graphql-authz/apollo-server-plugin";;
const server = new ApolloServer({
...
plugins: [authZApolloPlugin({ rules, authSchema })],
...
});