[英]AppSync HTTP resolver mapping template not working with method PATCH
我正在使用此 AppSync 解析器映射模板来更新用户:
{
"version": "2018-05-29",
"method": "PATCH",
"params": {
"headers": {
"Content-Type": "application/json"
},
"body": $utils.toJson(${context.arguments.input})
},
"resourcePath": $utils.toJson("/users/${context.arguments.input.id}")
}
后端中的服务接受更新用户的PATCH
请求。
当我运行突变时,我收到此错误: An internal failure occurred while resolving this field.
这是请求映射和响应映射的 CloudWatch 日志:
{
"logType": "RequestMapping",
"path": [
"updateUser"
],
"fieldName": "updateUser",
"resolverArn": "arn:aws:appsync:us-east-1:xxxxxx:apis/xxxxx/types/Mutation/resolvers/updateUser",
"requestId": "xxxxxx",
"context": {
"arguments": {
"input": {
"id": "user1",
"firstName": "John"
}
},
"stash": {},
"outErrors": []
},
"fieldInError": false,
"errors": [],
"parentType": "Mutation",
"graphQLAPIId": "xxxxxx",
"transformedTemplate": "{\n \"version\": \"2018-05-29\",\n \"method\": \"PATCH\",\n \"params\": {\n \"headers\": {\n \"Content-Type\": \"application/json\"\n },\n \"body\": {\"id\":\"user1\",\"firstName\":\"John\"}\n },\n \"resourcePath\": \"/users/user1\"\n}"
}
{
"logType": "ResponseMapping",
"path": [
"updateUser"
],
"fieldName": "updateUser",
"resolverArn": "arn:aws:appsync:us-east-1:xxxxx:apis/xxxxx/types/Mutation/resolvers/updateUser",
"requestId": "xxxxxx",
"context": {
"arguments": {
"input": {
"id": "user1",
"firstName": "John"
}
},
"stash": {},
"error": {
"message": "An internal failure occurred while resolving this field.",
"type": "InternalFailure"
},
"outErrors": []
},
"fieldInError": true,
"errors": [
"CustomTemplateException(message=A custom error was thrown from a mapping template., errorType=$context.result.statusCode, data=null, errorInfo=null)"
],
"parentType": "Mutation",
"graphQLAPIId": "xxxxxx"
}
请求不会转发到后端服务(我正在监视传入请求),但是如果我将映射中的方法更改为POST
或PUT
,我可以看到请求到达服务。
我还有其他需要PATCH
方法的请求,它们也有同样的问题。 我是否在PATCH
请求的解析器中遗漏了某些内容,或者这是一个 AppSync 错误?
正文需要是一个字符串( https://docs.aws.amazon.com/appsync/latest/devguide/resolver-mapping-template-reference-http.html ):
{
"version": "2018-05-29",
"method": "PUT|POST|GET|DELETE|PATCH",
"params": {
"query": Map,
"headers": Map,
"body": string
},
"resourcePath": string
}
在您转换后的请求模板中,它是一个 JSON 对象:
"transformedTemplate": "... \"body\": {\"id\":\"user1\",\"firstName\":\"John\"}\n },\n ..."
我不确定这是否是这里唯一的问题(错误消息没有解释太多)但这绝对是一个问题。
Appsync 似乎不接受PATCH
请求。
我使用 Postman 对 Cognito 授权的 Appsync API 确认了这一点。 PUT
和POST
返回预期结果。 相同的PATCH
请求返回 Appsync UnknownOperationException
错误。
我没有发现这有明确的记录,但是re:invent 演示确实从动词列表中省略了PATCH
(第 21 页?)。
您的上下文 object 没有状态字段$context.result.statusCode
; 如果你想在 vtl 模板中使用上下文,请改用ctx
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.