Hello,
I have just started using the javaScript AppSync resolvers after using VTL for years. One issue I am coming across is the util.error(...)
or util.appendError(...)
utility functions appear to have no effect.
My set-up currently includes a function to call and http data source, and a query which uses this function. My function calls an API Gateway REST api with a GET request. The response mapping template simply returns the parsed JSON, and if the response was not 200, call appendError.
import { util } from '@aws-appsync/utils';
export function request(ctx) {
return {
method: 'GET',
params: {
headers: {
'Content-Type': 'application/json',
'X-Api-Key': '<redacted>'
}
},
resourcePath: '/dev/<redacted>/' + ctx.stash.id + '.json'
};
}
export function response(ctx) {
if (ctx.result.statusCode !== 200){
return util.appendError(ctx.result.body, ctx.result.statusCode);
}
else {
return JSON.parse(ctx.result.body);
}
}
My query is as follows:
import { util } from '@aws-appsync/utils';
export function request(ctx) {
ctx.stash.id = ctx.args.id
return {};
}
export function response(ctx) {
return ctx.result;
}
Part of the the ResponseFunctionEvaluation in CloudWatch is as follows:
"context": {
"arguments": {
"id": "testr"
},
"result": {
"headers": {
"content-length": "38",
"Content-Type": "application/json",
...
"X-Cache": "Error from cloudfront"
},
"statusCode": 404,
"body": "{\"message\": \"That key does not exist\"}"
},
"prev": {
"result": {}
},
"stash": {
"id": "testr"
},
"outErrors": []
},
"fieldInError": false,
"errors": [],
And part of the AfterResponseFunctionEvaluation:
"context": {
"arguments": {
"id": "testr"
},
"prev": {},
"stash": {
"id": "testr"
},
"outErrors": []
},
"fieldInError": false,
"errors": [],
VTL resolvers appear to work correctly. I am not sure what I am doing wrong here?
I, too, lost hours to this same error. It's a silent failure. Even the @aws-appsync/base linter plugin did not catch it.