概要
パーティションキーとソートキーが設定されているDynamoDBにつながっているAppSyncのリゾルバー記述方法を紹介します。
前提条件
AppSync
スキーマ
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)
確認
正常に動くことを確認できました。