要使用AppSync进行批量更新项目,你可以使用AWS AppSync提供的GraphQL接口来执行批量更新操作。以下是一个示例解决方法:
mutation BatchUpdateItems($input: [UpdateItemInput!]!) {
batchUpdateItems(input: $input) {
updatedItems
}
}
const AWS = require('aws-sdk');
const appsync = new AWS.AppSync();
const itemsToUpdate = [
{ itemId: "1", name: "Item 1 Updated" },
{ itemId: "2", name: "Item 2 Updated" },
{ itemId: "3", name: "Item 3 Updated" }
];
const input = itemsToUpdate.map(item => ({
itemID: item.itemId,
data: {
name: { set: item.name }
}
}));
const params = {
mutation: `mutation BatchUpdateItems($input: [UpdateItemInput!]!) {
batchUpdateItems(input: $input) {
updatedItems
}
}`,
variables: {
input: input
}
};
appsync.graphql(params, (err, data) => {
if (err) {
console.log("Error", err);
} else {
console.log("Batch update success", data);
}
});
上述代码假设你已经配置了AWS SDK并创建了AppSync的实例。你需要将itemsToUpdate
数组替换为你要更新的项目列表,每个项目对象包含itemId
和name
属性。
请注意,上述示例中的mutation和变量参数与你的AppSync schema定义相关,确保根据你的schema进行相应的调整。