Graphql突变与aws-appsync上的父对象和子对象数组

时间:2019-07-15 16:25:52

标签: graphql aws-appsync

我得到了一个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)

对于创建答案我得到的功能:

请求映射模板:

#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)"
    }
  ]
}

1 个答案:

答案 0 :(得分:0)

您能否共享您的Mutation类型以及您正在使用的其他类型定义,以便我们可以帮助您重现?模板之前,您的管道解析器是什么?

也就是说,我看到了几处可能有问题的东西,首先,您的createQuestion请求映射模板,util调用缺少了$

  

“ attributeValues”:   ** $ ** util.dynamodb.toMapValuesJson($ ctx.args.input.question)

此外,在您的createAnswersFunction响应映射模板中,您尝试访问json字符串上的items属性。以下内容无效

  

$ util.toJson($ ctx.result).items

相反,您可能是想

$util.toJson($ctx.result.items)

但是这也将不起作用,因为BatchPutItem操作的结果是由表键入的,因此要从结果中获取数据,您应该这样做

$ctx.result.data.get("Answer-ium2s7hanfgl3dwbamwgloetsi-dev")

通常,在您进行开发时,建议您至少在Error级别启用CloudWatch Logs。这将为您提供field level log information,即您可以访问调用结果,评估的映射模板,存储的内容等。这对于调试很有帮助。