0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

AWS AppSyncでAPIを作ってみたAdvent Calendar 2022

Day 9

AppSyncのリゾルバーで加減算処理

Last updated at Posted at 2022-12-08

概要

DynamoDBの値を加減算するAppSyncのリゾルバーを紹介します。

前提条件

  • DynamoDBのテーブルが用意されていること
  • AppSyncのデータソースにDynamoDBのテーブルを設定していること

AppSyncのスキーマ

対応箇所のみピックアップ

type fuwa {
  id: ID!
  fuwa_num: Int
}

input UpdatefuwaInput {
  fuwa_num: Int
}

type Mutation {
  updatefuwa(id: ID!, add: UpdatefuwaInput): fuwa
}

AppSyncのリゾルバー

リクエストマッピングテンプレート

{
  "version": "2018-05-29",
  "operation": "UpdateItem",
  "key": {
    "id": $util.dynamodb.toDynamoDBJson($ctx.args.id),
  },

  ## Set up some space to keep track of things we're updating **
  #set( $expNames  = {} )
  #set( $expValues = {} )
  #set( $expSet = {} )
  #set( $expAdd = {} )
  
  ## addの処理
  #foreach( $entry in $ctx.args.add.entrySet() )
    $!{expAdd.put("#${entry.key}", ":ADD_${entry.key}")}
    $!{expNames.put("#${entry.key}", "${entry.key}")}
    $!{expValues.put(":ADD_${entry.key}", $util.dynamodb.toDynamoDB($entry.value))}
  #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

  ## 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(#id)",
    "expressionNames": {
      "#id": "id",
    },
  }
}

レスポンスマッピングテンプレート

$util.toJson($ctx.result)

実行結果

AppSync(加算)

  • 画面左が実行クエリ、画面右が実行結果
    クエリ

AppSync(減算)

  • 画面左が実行クエリ、画面右が実行結果
    クエリ

  • 実行結果には加減算した結果が返ってきます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?