本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
AWS AppSync 中的问题排查和常见错误
此部分将讨论一些常见的错误以及如何排查这些错误。
DynamoDB 键映射不正确
如果您的 GraphQL 操作返回以下错误消息,可能是因为您的请求映射模板结构与 Amazon DynamoDB 键结构不匹配:
The provided key element does not match the schema (Service: AmazonDynamoDBv2; Status Code: 400; Error Code
例如,如果您的 DynamoDB 表具有一个名为 "id"
的哈希键,并且您的模板包含 "PostID"
(如以下示例中所示),则会导致上述错误,因为 "id"
与 "PostID"
不匹配。
{ "version" : "2017-02-28", "operation" : "GetItem", "key" : { "PostID" : $util.dynamodb.toDynamoDBJson($ctx.args.id) } }
缺少解析器
如果您执行 GraphQL 操作(如查询)并获得 Null 响应,这可能是因为您没有配置解析器。
例如,如果您导入的架构定义了 getCustomer(userId:
ID!):
字段,但您尚未为此字段配置解析器,则当执行查询时(例如 getCustomer(userId:"ID123"){...}
),您将获得如下所示的响应:
{ "data": { "getCustomer": null } }
映射模板错误
如果您的映射模板未正确配置,您将收到其 errorType
为 MappingTemplate
的 GraphQL 响应。message
字段应指出问题出在映射模板中的何处。
例如,如果在您的请求映射模板中没有 operation
字段,或者 operation
字段名称不正确,您将收到类似以下内容的响应:
{ "data": { "searchPosts": null }, "errors": [ { "path": [ "searchPosts" ], "errorType": "MappingTemplate", "locations": [ { "line": 2, "column": 3 } ], "message": "Value for field '$[operation]' not found." } ] }
返回类型不正确
数据来源的返回类型必须与架构中对象的已定义类型匹配,否则您可能会看到如下所示的 GraphQL 错误:
"errors": [ { "path": [ "posts" ], "locations": null, "message": "Can't resolve value (/posts) : type mismatch error, expected type LIST, got OBJECT" } ]
例如,对于以下查询定义,可能会发生上述错误:
type Query { posts: [Post] }
这预计是 [Posts]
对象的 LIST。例如,如果 Node.JS 中有一个 Lambda 函数(与以下内容类似):
const result = { data: data.Items.map(item => { return item ; }) }; callback(err, result);
这将引发错误,因为 result
是一个对象。您需要将回调更改为 result.data
,或更改架构以不返回 LIST。
处理无效请求
当 AWS AppSync 由于数据不正确(例如语法无效)而无法处理请求并将其发送到字段解析器时,响应有效载荷将返回字段数据(值设置为 null
)以及任何相关错误。