Javascript 如果检查成功,JS decorator将返回原始方法
我已经为我的graphql变体设置了一个简单的装饰器,它所要做的就是对请求运行身份验证检查,看看它是否被授权。我让它运行检查并返回一个错误,如果一切按计划进行,但当没有错误时(请求被授权),它只是挂起 这是我的装饰代码:Javascript 如果检查成功,JS decorator将返回原始方法,javascript,babeljs,decorator,ecmascript-next,Javascript,Babeljs,Decorator,Ecmascript Next,我已经为我的graphql变体设置了一个简单的装饰器,它所要做的就是对请求运行身份验证检查,看看它是否被授权。我让它运行检查并返回一个错误,如果一切按计划进行,但当没有错误时(请求被授权),它只是挂起 这是我的装饰代码: function checkAuth(target) { target.mutateAndGetPayload = (input, { rootValue }) => { if (!rootValue.request.user) { return
function checkAuth(target) {
target.mutateAndGetPayload = (input, { rootValue }) => {
if (!rootValue.request.user) {
return {
error: {
message: `Invalid authorization header`,
type: "invalid_auth",
client_message: `You do not have permission to access this resource`,
}
};
}
return target.mutateAndGetPayload;
};
return target;
}
这是我的变异代码(只是一个简单的变异):
如果请求得到授权并且装饰程序没有返回错误,则它永远不会到达console.log。感谢您的帮助,因为我只使用过几次装饰师。我觉得这是我在
目标中返回的方式需要做的事情。mutateAndGetPayload
仅供参考,装饰师不是ES6的一部分。它们是一个建议,即尚未成为任何规范的一部分。@FelixKling Cool感谢您让我知道!
const AddMedicalRecord = mutationWithClientMutationId({
name: 'AddMedicalRecord',
description: 'AddMedicalRecord',
inputFields: {
...fields here...
},
outputFields: {
error: {
type: ErrorType, // Simply: { client_message, message, type }
description: 'Description of the error if anything',
resolve: payload => payload.error
},
success: {
type: GraphQLString,
description: 'Description of the success',
resolve: payload => payload.success
}
},
@checkAuth
mutateAndGetPayload: (input, { rootValue }) => {
console.log(input, 'passed decorator :D ') // It never reaches here with decorator