Home > Software design >  AppSync HTTP resolver mapping template not working with method PATCH
AppSync HTTP resolver mapping template not working with method PATCH

Time:10-28

I'm using this AppSync resolver mapping template to update a user:

{
    "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}")
}

The service in the backend accept a PATCH request to update the user.

When I run the mutation I get this error: An internal failure occurred while resolving this field.

This are the CloudWatch logs for the Request Mapping and Response Mapping:

{
    "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"
}

The request is not forwarded to the backend service (I'm monitoring incoming requests), however if I change the method in the mapping to POST or PUT I can see the request arriving to the service.

I have other requests that require a PATCH method and they have the same issue. Am I missing something in the resolver for PATCH requests or this is an AppSync bug?

CodePudding user response:

The body needs to be a String (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
}

In your transformed request template it is a JSON object:

"transformedTemplate": "... \"body\": {\"id\":\"user1\",\"firstName\":\"John\"}\n    },\n ..."

I'm not sure if that's the only problem here (the error message does not explain much) but this is definitely a problem.

CodePudding user response:

Appsync does not seem to accept PATCH requests.

I confirmed this using Postman against a Cognito-authorized Appsync API. PUT and POST return the expected results. Identical PATCH requests return with an Appsync UnknownOperationException error.

I did not find this explicitly documented, but a re:invent presentation does omit PATCH from its list of verbs (page 21?).

  • Related