

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 类型系统（响应映射）
<a name="aws-appsync-resolver-mapping-template-reference-dynamodb-typed-values-responses"></a>

收到来自 DynamoDB 的响应时 AWS AppSync ，会自动将其转换为 GraphQL 和 JSON 原始类型。将解码 DynamoDB 中的每个属性，并在响应映射上下文中返回。

例如，如果 DynamoDB 返回以下内容：

```
{
    "id" : { "S" : "1234" },
    "name" : { "S" : "Nadia" },
    "age" : { "N" : 25 }
}
```

然后， AWS AppSync DynamoDB 解析器将其转换为 GraphQL 和 JSON 类型，如下所示：

```
{
    "id" : "1234",
    "name" : "Nadia",
    "age" : 25
}
```

本节介绍如何 AWS AppSync 转换以下 DynamoDB 标量、文档和集合类型：

**字符串类型 `S` **  
单个字符串值。DynamoDB 字符串值以字符串形式返回。  
例如，如果 DynamoDB 返回以下 DynamoDB 字符串值：  

```
{ "S" : "some string" }
```
AWS AppSync 将其转换为字符串：  

```
"some string"
```

**字符串集类型 `SS` **  
一组字符串值。DynamoDB 字符串集值以字符串列表形式返回。  
例如，如果 DynamoDB 返回以下 DynamoDB 字符串集值：  

```
{ "SS" : [ "first value", "second value", ... ] }
```
AWS AppSync 将其转换为字符串列表：  

```
[ "+1 555 123 4567", "+1 555 234 5678" ]
```

**数字类型 `N` **  
单个数字值。DynamoDB 数字值以数字形式返回。  
例如，如果 DynamoDB 返回以下 DynamoDB 数字值：  

```
{ "N" : 1234 }
```
AWS AppSync 将其转换为数字：  

```
1234
```

**数字集类型 `NS` **  
一组数字值。DynamoDB 数字集值以数字列表形式返回。  
例如，如果 DynamoDB 返回以下 DynamoDB 数字集值：  

```
{ "NS" : [ 67.8, 12.2, 70 ] }
```
AWS AppSync 将其转换为数字列表：  

```
[ 67.8, 12.2, 70 ]
```

**二进制类型 `B` **  
二进制值。DynamoDB 二进制值以字符串形式返回，其中包含该值的 Base64 表示形式。  
例如，如果 DynamoDB 返回以下 DynamoDB 二进制值：  

```
{ "B" : "SGVsbG8sIFdvcmxkIQo=" }
```
AWS AppSync 将其转换为包含该值的 base64 表示形式的字符串：  

```
"SGVsbG8sIFdvcmxkIQo="
```
请注意，二进制数据按照 [RFC 4648](https://tools.ietf.org/html/rfc4648) 和 [RFC 2045](https://tools.ietf.org/html/rfc2045) 中指定的方式根据 base64 编码方案进行编码。

**二进制集类型 `BS` **  
一组二进制值。DynamoDB 二进制集值以字符串列表形式返回，其中包含这些值的 Base64 表示形式。  
例如，如果 DynamoDB 返回以下 DynamoDB 二进制集值：  

```
{ "BS" : [ "SGVsbG8sIFdvcmxkIQo=", "SG93IGFyZSB5b3U/Cg==" ... ] }
```
AWS AppSync 将其转换为包含以下值的 base64 表示形式的字符串列表：  

```
[ "SGVsbG8sIFdvcmxkIQo=", "SG93IGFyZSB5b3U/Cg==" ... ]
```
请注意，二进制数据按照 [RFC 4648](https://tools.ietf.org/html/rfc4648) 和 [RFC 2045](https://tools.ietf.org/html/rfc2045) 中指定的方式根据 base64 编码方案进行编码。

**布尔值类型 `BOOL` **  
布尔值。DynamoDB 布尔值以布尔值形式返回。  
例如，如果 DynamoDB 返回以下 DynamoDB 布尔值：  

```
{ "BOOL" : true }
```
AWS AppSync 将其转换为布尔值：  

```
true
```

**列表类型 `L` **  
任何其他支持的 DynamoDB 值列表。DynamoDB 列表值以值列表形式返回，其中还会转换每个内部值。  
例如，如果 DynamoDB 返回以下 DynamoDB 列表值：  

```
{ "L" : [
      { "S"  : "A string value" },
      { "N"  : 1 },
      { "SS" : [ "Another string value", "Even more string values!" ] }
   ]
}
```
AWS AppSync 将其转换为转换后的值列表：  

```
[ "A string value", 1, [ "Another string value", "Even more string values!" ] ]
```

**映射类型 `M` **  
任何其他受支持的 DynamoDB 值的 key/value 集合。DynamoDB 地图值以 JSON 对象的形式返回， key/value 每个值也将在其中进行转换。  
例如，如果 DynamoDB 返回以下 DynamoDB 映射值：  

```
{ "M" : {
      "someString" : { "S"  : "A string value" },
      "someNumber" : { "N"  : 1 },
      "stringSet"  : { "SS" : [ "Another string value", "Even more string values!" ] }
   }
}
```
AWS AppSync 将其转换为 JSON 对象：  

```
{
   "someString" : "A string value",
   "someNumber" : 1,
   "stringSet"  : [ "Another string value", "Even more string values!" ]
}
```

**Null 类型 `NULL` **  
Null 值。  
例如，如果 DynamoDB 返回以下 DynamoDB Null 值：  

```
{ "NULL" : null }
```
AWS AppSync 将其转换为空值：  

```
null
```