

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# TransactGetItems
<a name="aws-appsync-resolver-mapping-template-reference-dynamodb-transact-get-items"></a>

`TransactGetItems` 請求映射文件可讓您指示 AWS AppSync DynamoDB 解析程式向 DynamoDB 提出`TransactGetItems`請求，以擷取可能跨多個資料表的多個項目。使用此要求範本時，您必須指定下列項目：
+ 從中擷取項目之每個請求項目的資料表名稱
+ 從每個資料表擷取之每個請求項目的索引鍵

此時會套用 DynamoDB `TransactGetItems` 限制，且可能不會提供**任何條件表達式**。

`TransactGetItems` 映射文件結構如下：

```
{
    "version": "2018-05-29",
    "operation": "TransactGetItems",
    "transactItems": [
       ## First request item
       {
           "table": "table1",
           "key": {
               "foo": ... typed value,
               "bar": ... typed value
           },
           "projection" : {
                ...
           }
       },
       ## Second request item
       {
           "table": "table2",
           "key": {
               "foo": ... typed value,
               "bar": ... typed value
           },
           "projection" : {
                ...
           }
       }
    ]
}
```

欄位定義如下：

## TransactGetItems 欄位
<a name="TransactGetItems-list"></a>

### TransactGetItems 欄位清單
<a name="TransactGetItems-list-col"></a>

** `version` **  
範本定義的版本。僅支援 `2018-05-29`。此值為必填。

** `operation` **  
要執行的 DynamoDB 操作。若要執行 `TransactGetItems` DynamoDB 操作，這必須設為 `TransactGetItems`。此值為必填。

** `transactItems` **  
要包含的請求項目。此值是請求項目的陣列。必須提供至少一個請求項目。`transactItems` 值為必填。    
** `table` **  
要從中擷取項目的 DynamoDB 資料表。此值是資料表名稱的字串。`table` 值為必填。  
** `key` **  
代表要擷取之項目主索引鍵的 DynamoDB 索引鍵。DynamoDB 項目可能具有單一雜湊索引鍵，或雜湊索引鍵和排序索引鍵，視資料表結構而定。如需如何指定「輸入值」的詳細資訊，請參閱[類型系統 （請求映射）](aws-appsync-resolver-mapping-template-reference-dynamodb-typed-values-request.md)。  
**`projection`**  
用於指定要從 DynamoDB 操作傳回之屬性的投影。如需投影的詳細資訊，請參閱[投影](https://docs.aws.amazon.com/appsync/latest/devguide/resolver-mapping-template-reference-dynamodb.html#aws-appsync-resolver-mapping-template-reference-dynamodb-projections)。此欄位為選用欄位。

注意事項：
+ 如果交易成功，`items` 區塊中擷取項目的順序會與請求項目的順序相同。
+ 以全有或全無的方式執行交易。如果任何請求項目造成錯誤，將不執行整個交易，而且將傳回錯誤詳細資料。
+ 無法擷取的請求項目不是錯誤。相反地，*null* 元素會出現在對應位置的*項目*區塊。
+ 如果交易錯誤是 *TransactionCanceledException*，則會填入 `cancellationReasons` 區塊。`cancellationReasons` 區塊中取消原因的順序將與請求項目的順序相同。
+  `TransactGetItems` 限制為 100 個請求項目。

使用下列範例要求映射範本時：

```
{
    "version": "2018-05-29",
    "operation": "TransactGetItems",
    "transactItems": [
       ## First request item
       {
           "table": "posts",
           "key": {
               "post_id": {
                 "S": "p1"
               }
           }
       },
       ## Second request item
       {
           "table": "authors",
           "key": {
               "author_id": {
                 "S": a1
               }
           }
       }
    ]
}
```

如果交易成功，而且僅擷取第一個請求項目，則 `$ctx.result` 中的可用叫用結果如下所示：

```
{
    "items": [
       {
           // Attributes of the first requested item
           "post_id": "p1",
           "post_title": "title",
           "post_description": "description"
       },
       // Could not retrieve the second requested item
       null,
    ],
    "cancellationReasons": null
}
```

如果因為第一個請求項目造成的 *TransactionCanceledException* 造成交易失敗，則 `$ctx.result` 中的可用叫用結果如下所示：

```
{
    "items": null,
    "cancellationReasons": [
       {
           "type":"Sample error type",
           "message":"Sample error message"
       },
       {
           "type":"None",
           "message":"None"
       }
    ]
}
```

`$ctx.error` 包含錯誤的詳細資訊。索引鍵 **items** 和 **cancellationReasons** 保證出現在 `$ctx.result` 中。

如需更完整的範例，請依照 DynamoDB 交易教學搭配 AppSync here [Tutorial： DynamoDB 交易解析程式](tutorial-dynamodb-transact.md#aws-appsync-tutorial-dynamodb-transact)。