0

我正在使用此 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 错误?

4

2 回答 2

0

正文需要是一个字符串(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 ..."

我不确定这是否是这里唯一的问题(错误消息没有解释太多)但这绝对是一个问题。

于 2021-10-28T08:29:18.883 回答
0

Appsync 似乎不接受PATCH请求。

我使用 Postman 针对 Cognito 授权的 Appsync API 确认了这一点。 PUTPOST返回预期结果。相同PATCH的请求返回 AppsyncUnknownOperationException错误。

我没有发现这明确记录,但re:invent 演示PATCH确实从其动词列表中省略(第 21 页?)。

于 2021-10-28T11:31:38.647 回答