![](/img/trans.png)
[英]How to write middleware for graphql which will be call before every resolver
[英]GraphQL resolver middleware for auth check
我有一个GraphQL API,其中混合了公共和私有查询以及变异。 我正在寻找一种检查操作是否需要对用户进行身份验证以及权限检查器的方法,以便用户只能修改自己的数据。
我注意到,解析器函数有第四个参数, info
包含path.key
,该path.key
返回操作的名称( 在此处记录 )。
我的解决方案是在每个解析器中添加一个检查器函数,如下所示:
// modify user details
resolve: async (parent, args, { mongo: { User }, loggedUser }, info) => {
// auth check
authChecker(info.path.key, loggedUser, args.id);
// continue resolving
},
并在另一个文件中:
function authChecker(operationName, loggedUser, userId) {
if (PUBLIC_OPERATIONS.includes(operationName) {
// public operation
return true;
} else {
// private operation
if (args.id) {
// private operation that requires a permission check
...
} else {
// private operation that only requires user to be logged in
...
}
}
}
如果不满足条件,该函数将返回true或引发错误。
我想知道这是否是一个好的解决方案,或者是否可以通过某种方式使用中间件来做到这一点,这样我就不必在每个解析器中都重复代码了。 问题是,如果我使用中间件,我将无权访问操作名称。 有什么建议么?
使用中间件应该是可能的,但是会很痛苦,因为您必须自己解析查询。 我认为最干净的方法是利用schemaql-tools提供的模式级解析器。
const {makeExecutableSchema, addSchemaLevelResolveFunction} = require('graphql-tools')
const schema = makeExecutableSchema({typeDefs, resolvers})
addSchemaLevelResolveFunction(schema, (root, args, context, info) => {
// check info to see if query is private and throw if user isn't authenticated
})
// use the schema like normal
app.use('/graphql', graphqlHTTP({schema}))
解析器不需要返回任何东西; 它只需要在身份验证失败时抛出或返回被拒绝的Promise。 有关使用graphql-tools生成模式的更多信息,请在此处查看文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.