在AWS Lambda部署的Apollo GraphQL中未处理授权标头

时间:2020-08-12 12:30:08

标签: https aws-lambda authorization apollo graphql-mutation

我有以下突变:

updatePoint(id: String! data: PointInput!): Result @auth(requires: "POINT_CRUD")

用于处理此问题的AuthDirective:

class AuthDirective extends SchemaDirectiveVisitor {
    visitFieldDefinition(field) {
        const requiredRole = this.args.requires;
        const originalResolve = field.resolve || defaultFieldResolver;
        const roleChecker = new RoleChecker();

        field.resolve = async function(...args) {

            const context = args[2];
            const isAuthorized = await roleChecker.check(context.authorization, requiredRole);

            if (!isAuthorized) {
                throw new AuthenticationError(`You need following role: ${requiredRole}`);
            }

            return originalResolve.apply(this, args);
        }
    }
}

我在操场上称变异为

mutation {
  updatePoint(id: "poppels", data: { id: "poppels", json: "session" }) {
    error
  }
}

AuthDirective在Playground的本地(http)正常运行,但是当我将其部署到AWS Lambda(https)时,似乎没有考虑带有授权令牌的标头或与其他标头发生冲突。当我在本地请求中不包含任何标头时,我会看到相同的错误:

{
  "errors": [
    {
      "message": "One or more parameter values are not valid. The AttributeValue for a key attribute cannot contain an empty string value. Key: id",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "updatePoint"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "message": "One or more parameter values are not valid. The AttributeValue for a key attribute cannot contain an empty string value. Key: id",
          "code": "ValidationException",
          "time": "2020-08-12T12:13:49.683Z",
          "requestId": "UB5MJC41BMSJABATLVQAURKDVRVV4KQNSO5AEMVJF66Q9ASUAAJG",
          "statusCode": 400,
          "retryable": false,
          "retryDelay": 22.224996180111013,
          "stacktrace": [
            "ValidationException: One or more parameter values are not valid. The AttributeValue for a key attribute cannot contain an empty string value. Key: id",
            "    at Request.extractError (/var/task/node_modules/aws-sdk/lib/protocol/json.js:51:27)",
            "    at Request.callListeners (/var/task/node_modules/aws-sdk/lib/sequential_executor.js:106:20)",
            "    at Request.emit (/var/task/node_modules/aws-sdk/lib/sequential_executor.js:78:10)",
            "    at Request.emit (/var/task/node_modules/aws-sdk/lib/request.js:688:14)",
            "    at Request.transition (/var/task/node_modules/aws-sdk/lib/request.js:22:10)",
            "    at AcceptorStateMachine.runTo (/var/task/node_modules/aws-sdk/lib/state_machine.js:14:12)",
            "    at /var/task/node_modules/aws-sdk/lib/state_machine.js:26:10",
            "    at Request.<anonymous> (/var/task/node_modules/aws-sdk/lib/request.js:38:9)",
            "    at Request.<anonymous> (/var/task/node_modules/aws-sdk/lib/request.js:690:12)",
            "    at Request.callListeners (/var/task/node_modules/aws-sdk/lib/sequential_executor.js:116:18)"
          ]
        }
      }
    }
  ],
  "data": {
    "updatePoint": null
  }
}

有什么想法为什么会这样?是我缺少的Lambda配置,还是创建Apollo服务器的方式?

0 个答案:

没有答案