AWS AppSync 에서 DynamoDB의 테이블을 연결해서 autoId 적용하기

AppSync에서 API 만들 때 같이 만든 테이블은 create api 호출할 때 id를 안 넣어도 자동으로 생성하던데, 기존에 있던 테이블을 데이터 원본(data source) 에서 연결을 하면 create api 호출할 때 id를 넣지 않으면 이런 에러가 난다.
One or more parameter values were invalid: Type mismatch for key id expected: S actual: NULL

{
  "data": {
    "createPost": null
  },
  "errors": [
    {
      "path": [
        "createPost"
      ],
      "data": null,
      "errorType": "DynamoDB:DynamoDbException",
      "errorInfo": null,
      "locations": [
        {
          "line": 10,
          "column": 3,
          "sourceName": null
        }
      ],
      "message": "One or more parameter values were invalid: Type mismatch for key id expected: S actual: NULL (Service: DynamoDb, Status Code: 400, Request ID: AGSPVS9P4SI5AU2BPDOAAGR1BRVV4KQNSO5AEMVJF66Q9ASUAAJG)"
    }
  ]
}

이 때는 스키마(Scheme) – 오른쪽의 해석기(Resolver) – Mutation 으로 간다. 여기에는 API 를 만들 때 같이 만든 테이블이 있는데 이 중에 createUser에 들어가면 이런 식으로 나온다.

{
  "version": "2017-02-28",
  "operation": "PutItem",
  "key": {
    "id": $util.dynamodb.toDynamoDBJson($util.autoId()),
  },
  "attributeValues": $util.dynamodb.toMapValuesJson($ctx.args.input),
  "condition": {
    "expression": "attribute_not_exists(#id)",
    "expressionNames": {
      "#id": "id",
    },
  },
}

하지만 기존에 있던 테이블을 연결했다면 이렇게 나온다.

{
  "version": "2017-02-28",
  "operation": "PutItem",
  "key": {
    "id": $util.dynamodb.toDynamoDBJson($ctx.args.input.id),
  },
  "attributeValues": $util.dynamodb.toMapValuesJson($ctx.args.input),
  "condition": {
    "expression": "attribute_not_exists(#id)",
    "expressionNames": {
      "#id": "id",
    },
  },
}

$ctx.args.input.id$util.autoId() 로 바꿔야 정상적으로 동작한다.