キーが2つあるDBにつながっているAppSyncのリゾルバー記述方法

# 概要
パーティションキーとソートキーが設定されているDynamoDBにつながっているAppSyncのリゾルバー記述方法を紹介します。

# 前提条件
  - DynamoDBにテーブルが用意されていること
      ![DynamoDB](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2318120/9b0c92c4-53ab-b3cd-dff2-dbb39bb566a5.png)

# AppSync
## スキーマ
  ```graphql
    input CreateRadioInput {
    	broadcaster: String!
    	program_name: String
    	cast: [String!]
    	datetime: String!
    	favorite: Boolean
    }
    
    input DeleteRadioInput {
    	broadcaster: String!
    	datetime: String!
    }
    
    type Mutation {
    	createRadio(input: CreateRadioInput!): Radio
    	updateRadio(input: UpdateRadioInput!): Radio
    	deleteRadio(input: DeleteRadioInput!): Radio
    }
    
    type Query {
    	getRadio(broadcaster: String!, datetime: String!): Radio
    }
    
    type Radio {
    	broadcaster: String!
    	program_name: String
    	cast: [String!]
    	datetime: String!
    	favorite: Boolean
    }
    
    input UpdateRadioInput {
    	broadcaster: String!
    	program_name: String
    	cast: [String!]
    	datetime: String!
    	favorite: Boolean
    }
  ```

## リゾルバー
### CREATE
  ```
    {
      "version": "2017-02-28",
      "operation": "PutItem",
      "key": {
        "broadcaster": $util.dynamodb.toDynamoDBJson($ctx.args.input.broadcaster),
        "datetime": $util.dynamodb.toDynamoDBJson($ctx.args.input.datetime),
      },
      "attributeValues": $util.dynamodb.toMapValuesJson($ctx.args.input),
      "condition": {
        "expression": "attribute_not_exists(#broadcaster) AND attribute_not_exists(#datetime)",
        "expressionNames": {
          "#broadcaster": "broadcaster",
          "#datetime": "datetime",
        },
      },
    }
  ```

### READ
  ```
    {
      "version": "2017-02-28",
      "operation": "GetItem",
      "key": {
      "broadcaster": $util.dynamodb.toDynamoDBJson($ctx.args.broadcaster),
      "datetime": $util.dynamodb.toDynamoDBJson($ctx.args.datetime),
      },
    }
  ```

### UPDATE
  ```
    {
      "version": "2017-02-28",
      "operation": "UpdateItem",
      "key": {
        "broadcaster": $util.dynamodb.toDynamoDBJson($ctx.args.input.broadcaster),
        "datetime": $util.dynamodb.toDynamoDBJson($ctx.args.input.datetime),
      },
    
      ## Set up some space to keep track of things we're updating **
      #set( $expNames  = {} )
      #set( $expValues = {} )
      #set( $expSet = {} )
      #set( $expAdd = {} )
      #set( $expRemove = [] )
    
      ## Iterate through each argument, skipping keys **
      #foreach( $entry in $util.map.copyAndRemoveAllKeys($ctx.args.input, ["broadcaster", "datetime"]).entrySet() )
        #if( $util.isNull($entry.value) )
          ## If the argument is set to "null", then remove that attribute from the item in DynamoDB **
    
          #set( $discard = ${expRemove.add("#${entry.key}")} )
          $!{expNames.put("#${entry.key}", "${entry.key}")}
        #else
          ## Otherwise set (or update) the attribute on the item in DynamoDB **
    
          $!{expSet.put("#${entry.key}", ":${entry.key}")}
          $!{expNames.put("#${entry.key}", "${entry.key}")}
          $!{expValues.put(":${entry.key}", $util.dynamodb.toDynamoDB($entry.value))}
        #end
      #end
    
      ## Start building the update expression, starting with attributes we're going to SET **
      #set( $expression = "" )
      #if( !${expSet.isEmpty()} )
        #set( $expression = "SET" )
        #foreach( $entry in $expSet.entrySet() )
          #set( $expression = "${expression} ${entry.key} = ${entry.value}" )
          #if ( $foreach.hasNext )
            #set( $expression = "${expression}," )
          #end
        #end
      #end
    
      ## Continue building the update expression, adding attributes we're going to ADD **
      #if( !${expAdd.isEmpty()} )
        #set( $expression = "${expression} ADD" )
        #foreach( $entry in $expAdd.entrySet() )
          #set( $expression = "${expression} ${entry.key} ${entry.value}" )
          #if ( $foreach.hasNext )
            #set( $expression = "${expression}," )
          #end
        #end
      #end
    
      ## Continue building the update expression, adding attributes we're going to REMOVE **
      #if( !${expRemove.isEmpty()} )
        #set( $expression = "${expression} REMOVE" )
    
        #foreach( $entry in $expRemove )
          #set( $expression = "${expression} ${entry}" )
          #if ( $foreach.hasNext )
            #set( $expression = "${expression}," )
          #end
        #end
      #end
    
      ## Finally, write the update expression into the document, along with any expressionNames and expressionValues **
      "update": {
        "expression": "${expression}",
        #if( !${expNames.isEmpty()} )
          "expressionNames": $utils.toJson($expNames),
        #end
        #if( !${expValues.isEmpty()} )
          "expressionValues": $utils.toJson($expValues),
        #end
      },
    
      "condition": {
        "expression": "attribute_exists(#broadcaster) AND attribute_exists(#datetime)",
        "expressionNames": {
          "#broadcaster": "broadcaster",
          "#datetime": "datetime",
        },
      }
    }
  ```

### DELETE
  ```
    {
      "version": "2017-02-28",
      "operation": "DeleteItem",
      "key": {
        "broadcaster": $util.dynamodb.toDynamoDBJson($ctx.args.input.broadcaster),
        "datetime": $util.dynamodb.toDynamoDBJson($ctx.args.input.datetime),
      },
    }
  ```

### レスポンスマッピングテンプレート
  レスポンスマッピングテンプレートはCRUDすべて共通して下記を設定してください。
  ```
    $util.toJson($ctx.result)
  ```

# 確認
  - CREATE
      ![CREATE](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2318120/4deb1ee0-17b6-c3b1-1a67-237c18a75007.png)
  - UPDATE
      ![UPDATE](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2318120/50e6d1cb-b081-4655-efa1-552caba7ee35.png)
  - READ
      ![READ](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2318120/dd46decc-6b15-6ec1-cc94-cb0a67a25baa.png)
  - DELETE
      ![DELETE](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2318120/1a877112-e385-11cd-d34e-4dfffcc3db81.png)

  正常に動くことを確認できました。