如果检查成功,JS装饰器返回原始方法

时间:2017-06-10 00:55:53

标签: javascript babeljs decorator ecmascript-next

我已经为我的graphql突变设置了一个简单的装饰器,所有它应该是对请求运行auth检查以查看它是否被授权。我有它来运行检查&如果一切都按计划返回错误,但是当没有错误(请求被授权)时,它就会挂起。

这是我的装饰代码:

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;
}

这是我的突变代码(只是一个简单的突变):

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

如果请求被授权且装饰者没有返回错误,它永远不会到达console.log。任何帮助都表示赞赏,因为我只使用了装饰器几次。我觉得这是我在target.mutateAndGetPayload

内回归的方式

0 个答案:

没有答案