我正在尝试实现以下教程中描述的GrapgQL API。
GraphQL API using Serverless + AWS AppSync + DynamoDB + Lambda resolvers + Cognito. [Part 1]
我正在使用最新版本的无服务器和其他涉及的依赖项。 (请参阅package.json)我的操作系统是Manjaro 5.4.2-1。
我的问题是common-response.vtl无法离线。部署到AWS后一切正常。
* file: mapping-templates/common-response.vtl *
## If there is a datasource invocation error, we choose to raise the same error
## the field data will be set to null.
#if($ctx.error)
$util.error($ctx.error.message, $ctx.error.type, $ctx.result)
#end
$util.toJson($ctx.result)
* file: lambda-functions/book/listBooks/index.js *
exports.handler = async (event) => {
console.log({event});
const books = [
{
id: '1',
title: 'First mocked book'
},
{
id: '2',
title: 'Second mocked book'
}
];
console.log(books);
return books;
};
lambda解析器日志看起来不错
* console *
Serverless: dynamoDB started: http://localhost:62226/
listening for subscriptions at: ws://localhost:38643/
Serverless: AppSync started: http://localhost:62222/graphql
Serverless: Starting Offline: local/eu-west-1.
Serverless: Routes for listBooks:
Serverless: POST /{apiVersion}/functions/sls-appsync-demo-local-listBooks/invocations
Serverless: Offline [HTTP] listening on http://localhost:3001
Serverless: Enter "rp" to replay the last request
{ event: { arguments: {} } }
[
{ id: '1', title: 'First mocked book' },
{ id: '2', title: 'Second mocked book' }
]
{ event: { arguments: {} } }
[
{ id: '1', title: 'First mocked book' },
{ id: '2', title: 'Second mocked book' }
]
但是邮递员的回复是一个错误
* postman *
{
"errors": [
{
"message": "JSON5: invalid character '&' at 2:3",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"listBooks"
]
}
],
"data": {
"listBooks": null
}
}
当我将记录的响应粘贴到解析器中时,邮递员会打印出预期的结果。我还尝试将结果放入错误响应中。
* file: mapping-templates/common-response.vtl *
## If there is a datasource invocation error, we choose to raise the same error
## the field data will be set to null.
#if($ctx.error)
$util.error($ctx.error.message, $ctx.error.type, $ctx.result)
#end
$util.error($ctx.result)
结果在那里...
* postman *
{
"data": {
"listBooks": null
},
"errors": [
{
"message": [
{
"id": "1",
"title": "First mocked book"
},
{
"id": "2",
"title": "Second mocked book"
}
],
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"listBooks"
],
"errorType": null,
"data": null,
"errorInfo": null
}
]
}
因此toJson函数与脱机appsync结合使用时出现问题,但我不知道如何解决此问题
完整的示例代码:github repo