'Creating Batch Operations with AWS Amplify [GraphQL, DataStore, AppSync]

I've currently been handling batch operations with a for loop, but obviously, this is not the best approach, especially as I'm adding an 'upload by CSV' option, which will take 1000+ putItems.

I searched around for the best ways to implement this, specifically this link: https://docs.aws.amazon.com/appsync/latest/devguide/tutorial-dynamodb-batch.html

However, even after following those steps mentioned I'm not able to achieve a batch operation. Below is my code for a 'batch delete' operation.

Here is my schema.graphql file:

type Client @model @auth(rules: [{ allow: owner }]) {
  id: ID!
  name: String!
  company: String
  phone: String
  email: String
}

type Mutation {
  batchDelete(ids: [ID]): [Client]
}

I then create two new files. One request mapping template and one response mapping template.

#set($clientsdata = [])
#foreach($item in ${ctx.args.clients})
    $util.qr($clientsdata.delete($util.dynamodb.toMapValues($item)))
#end

{
    "version" : "2018-05-29",
    "operation" : "BatchDeleteItem",
    "tables" : {
        "Clients": $utils.toJson($clientsdata)
    }
}

and then as per the tutorial a "simple pass through" response mapping template: $util.toJson($ctx.result.data.Posts)

However now when I run the batchdelete command, I keep getting nothing returned. Would really appreciate guidance on this!



Solution 1:[1]

When it comes to performing DynamoDB batch operations in tandem with Amplify, note that the table name specified in the schema is actually different per environment, i.e. your "Client" table wouldn't be recognized as "Clients" as you have stated it in the request mapping template, but rather the name it is given on Amplify push, per environment. E.g. Client-<some alphanumeric number>-envName

Add the full name of the table to your request and response mapping templates.

Also your foreach statement should read: #foreach($item in ${ctx.args.clientsdata}) wherein you iterate through each of the items in the array that is passed as the argument to the context object.

Hope this helps.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 danieljovan