以下是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
回到我身边?如果我要求作者或头衔在我的查询中返回,我根本不会让他们回来。
更新完整的羊羔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)
答案 0 :(得分:0)
如果没有看到所有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响应。