aws appsync上具有父对象和子对象数组的Graphql变异
我得到了一个graphql变异,它传递了一个父对象和一系列子对象,比如aws appsync上具有父对象和子对象数组的Graphql变异,graphql,aws-appsync,Graphql,Aws Appsync,我得到了一个graphql变异,它传递了一个父对象和一系列子对象,比如 input CreateQuestionWithManyAnswersInput { question: CreateQuestionInput! answers: [CreateAnswerInput!]! } 实际的类型很无聊,一个ID,一些字符串等等 我已经在appsync中为这个变异附加了一个管道解析器,但我不知道如何编写单独的请求和响应映射模板 到目前为止,我为createQuestion函数得到
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)
#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)
type Mutation {
createQuestionWithManyAnswers(input: CreateQuestionWithManyAnswersInput!): QuestionWithManyAnswers
}
type QuestionWithManyAnswers {
question: Question!
answers: [Answer]!
}
响应映射模板:
{
"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)
#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)
type Mutation {
createQuestionWithManyAnswers(input: CreateQuestionWithManyAnswersInput!): QuestionWithManyAnswers
}
type QuestionWithManyAnswers {
question: Question!
answers: [Answer]!
}
对于创建应答功能,我得到了:
请求映射模板:
{
"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)
#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)
type Mutation {
createQuestionWithManyAnswers(input: CreateQuestionWithManyAnswersInput!): QuestionWithManyAnswers
}
type QuestionWithManyAnswers {
question: Question!
answers: [Answer]!
}
响应映射模板:
{
"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)
#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)
type Mutation {
createQuestionWithManyAnswers(input: CreateQuestionWithManyAnswersInput!): QuestionWithManyAnswers
}
type QuestionWithManyAnswers {
question: Question!
answers: [Answer]!
}
在管道的映射后模板中,我得到:
$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
}
更新
在蒂努的帮助下,我走得更远了。基本上,我现在的问题归结为如何在响应映射模板中将隐藏转换为有效响应
我的日志
"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)"
}
]
}
突变类型:
{
"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)
#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)
type Mutation {
createQuestionWithManyAnswers(input: CreateQuestionWithManyAnswersInput!): QuestionWithManyAnswers
}
type QuestionWithManyAnswers {
question: Question!
answers: [Answer]!
}
有许多回答类型的问题:
{
"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)
#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)
type Mutation {
createQuestionWithManyAnswers(input: CreateQuestionWithManyAnswersInput!): QuestionWithManyAnswers
}
type QuestionWithManyAnswers {
question: Question!
answers: [Answer]!
}
您是否可以将您的突变类型与您正在使用的其他类型定义共享,以便我们帮助您复制?模板之前的管道解析器是什么 也就是说,我发现有两件事情可能会有问题,首先,您的
createQuestion
请求映射模板,util调用缺少一个$
:
“属性值”:
**$**util.dynamodb.toMapValuesJson($ctx.args.input.question)
另外,在createanswersffunction
response映射模板中,您试图访问json字符串上的items
属性。下面的方法行不通
$util.toJson($ctx.result).items
相反,你的意思可能是
$util.toJson($ctx.result.items)
但这也不起作用,因为BatchPutItem操作的结果由表设置键,所以要从结果中获取数据,您应该这样做
$ctx.result.data.get("Answer-ium2s7hanfgl3dwbamwgloetsi-dev")
一般来说,在开发过程中,我建议您至少在
Error
级别启用CloudWatch日志。这将为您提供,即您将有权访问调用结果、已评估的映射模板、存储内容等。这有助于调试。您是否可以将您的突变类型与您正在使用的其他类型定义共享,以便我们帮助您复制?模板之前的管道解析器是什么
也就是说,我发现有两件事情可能会有问题,首先,您的createQuestion
请求映射模板,util调用缺少一个$
:
“属性值”:
**$**util.dynamodb.toMapValuesJson($ctx.args.input.question)
另外,在createanswersffunction
response映射模板中,您试图访问json字符串上的items
属性。下面的方法行不通
$util.toJson($ctx.result).items
相反,你的意思可能是
$util.toJson($ctx.result.items)
但这也不起作用,因为BatchPutItem操作的结果由表设置键,所以要从结果中获取数据,您应该这样做
$ctx.result.data.get("Answer-ium2s7hanfgl3dwbamwgloetsi-dev")
一般来说,在开发过程中,我建议您至少在
Error
级别启用CloudWatch日志。这将为您提供,即您将有权访问调用结果、已评估的映射模板、存储内容等。这有助于调试。谢谢您的评论。我已经用你的建议更新了我的问题。我建议不要使用$ctx.result.put
,我们仍然不知道你的突变的输出类型。还可以在API上启用CloudWatch日志,以便查看after模板的输出。我本以为这是一个相当标准的操作。在表1中创建父对象,然后在表2中创建多个子对象。文档中是否没有这方面的工作示例?另一种方法是,在一系列api调用中在客户机上执行所有操作,这很可怕。此外,你说你会建议不要使用“put”。我还应该如何在管道解析器中持久化数据?iirc我直接从文件中删除了?谢谢你的评论Tinou。我已经用你的建议更新了我的问题。我建议不要使用$ctx.result.put
,我们仍然不知道你的突变的输出类型。还可以在API上启用CloudWatch日志,以便查看after模板的输出。我本以为这是一个相当标准的操作。在表1中创建父对象,然后在表2中创建多个子对象。文档中是否没有这方面的工作示例?另一种方法是,在一系列api调用中在客户机上执行所有操作,这很可怕。此外,你说你会建议不要使用“put”。我还应该如何在管道解析器中持久化数据?iirc我从文件中直接拿出来的?