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 10

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

Last updated at Posted at 2022-12-09

概要

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

前提条件

  • DynamoDBにテーブルが用意されていること
    DynamoDB

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)

確認

  • CREATE
    CREATE
  • UPDATE
    UPDATE
  • READ
    READ
  • DELETE
    DELETE

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

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?