Josh
Josh

Reputation: 2587

How do I delete multiple records from an AWS Amplify GraphQL API?

I've made a GraphQL API in an AWS Amplify React Native app. The API contains the model Transaction. AWS Amplify provides CRUD operations out of the box, and I can delete a single transaction no problem.

However, I would like to delete all transactions that meet certain criteria. How do I delete multiple transactions using this stack (AWS Amplify + GraphQL API, React Native)?

Upvotes: 1

Views: 2184

Answers (1)

LuckyTuvshee
LuckyTuvshee

Reputation: 1194

You can send 1 request with batch delete mutation.

using listTransaction to get filtered transactions.

const txnMutation: any = transactions.map((txn, i) => {
  return `mutation${i}: deleteTransaction(input: {id: "${txn.id}"}) { id }`;
});

await API.graphql(
  graphqlOperation(`
  mutation batchMutation {
    ${txnMutation}
  }
`)
);

import { API, graphqlOperation } from "aws-amplify";

Upvotes: 7

Related Questions