AWS AppSync에서 다른 모델 타입의 필드 사용하기

제목 짓기가 어려운데, 글의 작성자 정보를 같이 가져오는 거다. belongsTo 라고 해야하나? 아무튼 Post 테이블에 user: User를 추가한다.

type Post {
  id: ID!
  title: String
  content: String
  userId: String
  user: User
}

Scheme – Resolvers – Post – user: User – 연결(Attach) 를 눌러서 설정 화면으로 이동한다. 데이터 소소는 User 테이블 을 선택하고, request 에는 왼쪽의 템플릿에서 Get item by id를 선택하고 $ctx.args.id$ctx.source.userId로 수정한다.

{
    "version": "2017-02-28",
    "operation": "GetItem",
    "key": {
        "id": $util.dynamodb.toDynamoDBJson($ctx.source.userId),
    }
}

listPosts 에서 user를 추가하면 정보를 불러올 수 있다.

query listPosts {
  listPosts {
    items {
      content
      id
      title
      userId
      user {
        id
        name
        birthday
      }
    }
  }
}
{
  "data": {
    "listPosts": {
      "items": [
        {
          "content": "내용",
          "id": "11",
          "title": "제목",
          "userId": "a98f6a96-73c5-407e-b7b3-7b5b95da1b8e",
          "user": {
            "id": "a98f6a96-73c5-407e-b7b3-7b5b95da1b8e",
            "name": "Hello, world!",
            "birthday": "1970-01-01Z"
          }
        }
      ]
    }
  }
}