【问题标题】:AppSync HTTP resolver mapping template not working with method PATCHAppSync HTTP 解析器映射模板不适用于方法 PATCH
【发布时间】:2021-12-12 19:35:35
【问题描述】:

我正在使用此 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"
}

请求不会转发到后端服务(我正在监视传入的请求),但是如果我将映射中的方法更改为POSTPUT,我可以看到请求到达了服务。

我有其他需要 PATCH 方法的请求,他们也有同样的问题。 我是否在 PATCH 请求的解析器​​中遗漏了某些内容,或者这是 AppSync 错误?

【问题讨论】:

    标签: amazon-web-services aws-appsync aws-appsync-resolver


    【解决方案1】:

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

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

    【讨论】:

    • 这似乎不是问题。 $utils.toJson 接受一个对象并返回该对象的“字符串化” JSON 表示 (AWS doc)。无论如何,我也尝试过使用字符串,得到了相同的结果。
    【解决方案2】:

    Appsync 似乎不接受 PATCH 请求。

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

    我没有找到明确记录,但 a re:invent presentation 确实从其动词列表中省略了 PATCH(第 21 页?)。

    【讨论】:

    • 即使在docs PATCH 方法中提到你也可能是对的。
    猜你喜欢
    • 2020-10-19
    • 2020-05-29
    • 2019-09-01
    • 2020-02-17
    • 1970-01-01
    • 2020-08-19
    • 2019-05-21
    • 2019-11-04
    • 2019-07-19
    相关资源
    最近更新 更多