本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
中的故障診斷和常見錯誤 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 } }
映射範本錯誤
如果未正確設定映射範本,您將會收到 GraphQL 回應,其中的 errorType 為 MappingTemplate。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以及任何相關的錯誤。