我得到了一个graphql变异,它传递一个父对象以及一个类似
input CreateQuestionWithManyAnswersInput {
question: CreateQuestionInput!
answers: [CreateAnswerInput!]!
}实际的类型是相当无聊的,一个ID,一些字符串,等等。
我已经将管道解析器附加到appsync中的突变,但我不知道如何编写单独的请求和响应映射模板。
到目前为止,我在createQuestion函数中得到了这样的结果:
请求映射模板:
{
"operation": "PutItem",
"key": {
"id": $util.dynamodb.toDynamoDBJson($ctx.args.input.question.id),
},
"attributeValues" : $util.dynamodb.toMapValuesJson($ctx.args.input.question)
}响应映射模板:
$util.qr($ctx.stash.put("question", $util.toJson($ctx.result)))
$util.toJson($ctx.result)对于创建的answersFunction,我得到:
请求映射模板:
#set($answersdata = [])
#foreach($item in ${ctx.args.input.answers})
$util.qr($answersdata.add($util.dynamodb.toMapValues($item)))
#end
{
"operation" : "BatchPutItem",
"tables" : {
"Answer-ium2s7hanfgl3dwbamwgloetsi-dev": $utils.toJson($answersdata)
}
}响应映射模板:
$util.qr($ctx.stash.put("answers", $util.toJson($ctx.result).items))
$util.toJson($ctx.result)在中,在映射管道的模板之后,我得到了:
$util.qr($ctx.result.put("question", $ctx.stash.question))
$util.qr($ctx.result.put("answers", $ctx.stash.question.answers))
$util.toJson($ctx.result)但是,以下情况未能做到这一点:
{
"data": {
"createQuestionWithManyAnswers": null
},
"errors": [
{
"path": [
"createQuestionWithManyAnswers",
"question",
"id"
],
"locations": null,
"message": "Cannot return null for non-nullable type: 'ID' within parent 'Question' (/createQuestionWithManyAnswers/question/id)"
},
{
"path": [
"createQuestionWithManyAnswers",
"question",
"text"
],
"locations": null,
"message": "Cannot return null for non-nullable type: 'String' within parent 'Question' (/createQuestionWithManyAnswers/question/text)"
},
{
"path": [
"createQuestionWithManyAnswers",
"answers"
],
"locations": null,
"message": "Cannot return null for non-nullable type: 'null' within parent 'QuestionWithManyAnswers' (/createQuestionWithManyAnswers/answers)"
}
]
}突变型
type Mutation {
createQuestionWithManyAnswers(input: CreateQuestionWithManyAnswersInput!): QuestionWithManyAnswers
}在Tinou的帮助下,更新了,我得到了进一步的帮助。基本上,我现在的问题归结为如何在响应映射模板中将存储的内容转换为有效的响应?
我的日志
"stash": {
"answers": [
{
"correct": true,
"id": "0cbc20a3-09dd-44d8-bc7c-e2ce50d8f9b1",
"text": "a right answer"
},
{
"correct": false,
"id": "b73b5696-e86b-4ad5-bce4-765234f566df",
"text": "a wrong answer"
}
],
"question": {
"id": "b28b8c22-6cdb-41a1-8697-78a799deed6f",
"text": "my new title",
"explanation": "some explanation about this"
}
}我的回应是:
{
"data": {
"createQuestionWithManyAnswers": null
},
"errors": [
{
"path": [
"createQuestionWithManyAnswers",
"question"
],
"locations": null,
"message": "Cannot return null for non-nullable type: 'Question' within parent 'QuestionWithManyAnswers' (/createQuestionWithManyAnswers/question)"
},
{
"path": [
"createQuestionWithManyAnswers",
"answers"
],
"locations": null,
"message": "Cannot return null for non-nullable type: 'null' within parent 'QuestionWithManyAnswers' (/createQuestionWithManyAnswers/answers)"
}
]
}突变类型:
type Mutation {
createQuestionWithManyAnswers(input: CreateQuestionWithManyAnswersInput!): QuestionWithManyAnswers
}QuestionWithManyAnswers类型:
type QuestionWithManyAnswers {
question: Question!
answers: [Answer]!
}发布于 2019-07-15 17:52:54
你能和你正在使用的其他类型的定义一起分享你的突变类型吗?这样我们就可以帮助你复制了吗?模板之前的管道解析器是什么?
尽管如此,我看到了一些问题,首先,您的createQuestion请求映射模板,util调用缺少一个$:
“**$**util.dynamodb.toMapValuesJson($ctx.args.input.question)”:attributeValues
此外,在createAnswersFunction响应映射模板中,您试图访问json上的items属性。下面这些都不起作用
$util.toJson($ctx.result).items
相反,你可能是说
$util.toJson($ctx.result.items)但是这也不起作用,因为BatchPutItem操作的结果是由表键决定的,因此要从结果中获取数据,您应该这样做。
$ctx.result.data.get("Answer-ium2s7hanfgl3dwbamwgloetsi-dev")一般来说,在开发过程中,我建议您至少在CloudWatch级别启用Error日志。这将为您提供字段级日志信息,即您可以访问调用结果、评估的映射模板、存储内容等等。这对调试很有帮助。
https://stackoverflow.com/questions/57043793
复制相似问题