gpt4 book ai didi

node.js - graphql 解析器中的授权作为中间件

转载 作者:太空宇宙 更新时间:2023-11-04 01:30:04 25 4
gpt4 key购买 nike

我需要检查用户是否有权在解析器中执行此操作或执行此操作,但我不想在每个解析器中编写相同的代码。所以看来我应该使用装饰器。但我不知道如何在没有类的情况下使用装饰器。

jwt token 解码后的 UserId 存在于使用上下文的 req 对象中,我的解析器如下所示:

import { isAllowed } from 'somewhere';
export default {
Query: {
async q1(_, args, {req}) {
if (!isAllowed(req.userId,'action') throw new Error('Not Authorized!');
},
...
}

我心里有这样的想法:

export default {
Query: {
@isAllowed
async q1(_, args, {req}) {
// ...
},

// this method doesn't need authorization.
async q2(_,args, {req}) {

}
}```

But i don't know how to implement it.

最佳答案

我发现我应该使用架构指令来处理授权而不是解析器方法装饰。

关于node.js - graphql 解析器中的授权作为中间件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56394191/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com