AWS appsync with/Lambda 函数后端,GraphQL 对查询给出意外响应
AWS appsync w/ Lambd function backend, GraphQL gives unexpected repsonce to Query
接下来的内容都在AWS控制台中。我的 lambda 函数定义 deletePost
case "deletePost":
var id = event.arguments.id;
callback(null, {id:-1,author:"me",title:"title"}); //note, regardless of what your args are right now it is returning id:-1
break;
我的架构是
type Mutation {
...
deletePost(id: ID!): Post!
}
type Post {
id: ID!
author: String!
title: String
}
我的 graphiQL 查询是
mutation DeletePost{
deletePost(id: 3){
id
}
}
出于某种原因,当我将 id 硬编码为 -1 时,它只是鹦鹉学舌地 id=3
返回给我?如果我在查询中要求作者或标题为 return,我根本不会回复它们。
更新完整的羔羊 fxn。只是 aws appSync 文档提供的模板的略微修改版本。
exports.handler = (event, context, callback) => {
console.log("Received event {}", JSON.stringify(event, 3));
var posts = { //in memory array store (simulates DB)
"1": {"id": "1", "title": "First book", "author": "Author1"},
"2": {"id": "2", "title": "Second book", "author": "Author2"},
"3": {"id": "3", "title": "Third book", "author": "Author3"},
"4": {"id": "4", "title": "Fourth book", "author": "Author4"},
"5": {"id": "5", "title": "Fifth book", "author": "Author5"} };
console.log("Got an Invoke Request: "+event.field);
switch(event.field) {
case "getPost":
var id = event.arguments.id;
callback(null, posts[id]);
break;
case "updatePost":
posts[event.arguments.id]=event.arguments;
console.log(posts);
callback(null, event.arguments);
break;
case "deletePost":
var id = event.arguments.id;
//delete posts[event.arguments.id];
callback(null, {id:-1,author:"me",title:"tits"});
break;
case "allPosts":
var values = [];
for(var d in posts){
values.push(posts[d]);
}
callback(null, values);
break;
case "addPost":
var id = event.arguments.id;
posts[id]=event.arguments;
console.log(posts);
callback(null, event.arguments);
break;
case "addPostErrorWithData":
var id = event.arguments.id;
var result = posts[id];
// attached additional error information to the post
result.errorMessage = 'Error with the mutation, data has changed';
result.errorType = 'MUTATION_ERROR';
callback(null, result);
break;
default:
callback("Unknown field, unable to resolve" + event.field, null);
break;
}
};
解析器。大多数情况下直接传递数据。
#request mapping
{
"version" : "2017-02-28",
"operation": "Invoke",
"payload": {
"field": "addPost",
"arguments": $util.toJson($context.arguments)
}
}
#responce mapping
$util.toJson($context.result)
如果不查看所有 Lambda 函数以及您的解析器模板,就很难知道。从上面的代码我猜你使用本教程作为起点:https://docs.aws.amazon.com/appsync/latest/devguide/tutorial-lambda-resolvers.html
假设您在响应模板中传回结果,例如 $util.toJson($context.result)
,那么问题很可能与您的 Lambda 函数中的回调结构有关
let result = {"id" :-1, "author":"me", "title":"title"}
callback(null, result);
本质上,您需要传回一个结果对象,以便在 $context
对象中看到它并转换为 GraphQL JSON 响应给调用者。
接下来的内容都在AWS控制台中。我的 lambda 函数定义 deletePost
case "deletePost":
var id = event.arguments.id;
callback(null, {id:-1,author:"me",title:"title"}); //note, regardless of what your args are right now it is returning id:-1
break;
我的架构是
type Mutation {
...
deletePost(id: ID!): Post!
}
type Post {
id: ID!
author: String!
title: String
}
我的 graphiQL 查询是
mutation DeletePost{
deletePost(id: 3){
id
}
}
出于某种原因,当我将 id 硬编码为 -1 时,它只是鹦鹉学舌地 id=3
返回给我?如果我在查询中要求作者或标题为 return,我根本不会回复它们。
更新完整的羔羊 fxn。只是 aws appSync 文档提供的模板的略微修改版本。
exports.handler = (event, context, callback) => {
console.log("Received event {}", JSON.stringify(event, 3));
var posts = { //in memory array store (simulates DB)
"1": {"id": "1", "title": "First book", "author": "Author1"},
"2": {"id": "2", "title": "Second book", "author": "Author2"},
"3": {"id": "3", "title": "Third book", "author": "Author3"},
"4": {"id": "4", "title": "Fourth book", "author": "Author4"},
"5": {"id": "5", "title": "Fifth book", "author": "Author5"} };
console.log("Got an Invoke Request: "+event.field);
switch(event.field) {
case "getPost":
var id = event.arguments.id;
callback(null, posts[id]);
break;
case "updatePost":
posts[event.arguments.id]=event.arguments;
console.log(posts);
callback(null, event.arguments);
break;
case "deletePost":
var id = event.arguments.id;
//delete posts[event.arguments.id];
callback(null, {id:-1,author:"me",title:"tits"});
break;
case "allPosts":
var values = [];
for(var d in posts){
values.push(posts[d]);
}
callback(null, values);
break;
case "addPost":
var id = event.arguments.id;
posts[id]=event.arguments;
console.log(posts);
callback(null, event.arguments);
break;
case "addPostErrorWithData":
var id = event.arguments.id;
var result = posts[id];
// attached additional error information to the post
result.errorMessage = 'Error with the mutation, data has changed';
result.errorType = 'MUTATION_ERROR';
callback(null, result);
break;
default:
callback("Unknown field, unable to resolve" + event.field, null);
break;
}
};
解析器。大多数情况下直接传递数据。
#request mapping
{
"version" : "2017-02-28",
"operation": "Invoke",
"payload": {
"field": "addPost",
"arguments": $util.toJson($context.arguments)
}
}
#responce mapping
$util.toJson($context.result)
如果不查看所有 Lambda 函数以及您的解析器模板,就很难知道。从上面的代码我猜你使用本教程作为起点:https://docs.aws.amazon.com/appsync/latest/devguide/tutorial-lambda-resolvers.html
假设您在响应模板中传回结果,例如 $util.toJson($context.result)
,那么问题很可能与您的 Lambda 函数中的回调结构有关
let result = {"id" :-1, "author":"me", "title":"title"}
callback(null, result);
本质上,您需要传回一个结果对象,以便在 $context
对象中看到它并转换为 GraphQL JSON 响应给调用者。