首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >aws-appsync上带有父对象和子数组的Graphql变异

aws-appsync上带有父对象和子数组的Graphql变异
EN

Stack Overflow用户
提问于 2019-07-15 16:25:52
回答 1查看 1.3K关注 0票数 1

我得到了一个graphql变异,它传递一个父对象以及一个类似

代码语言:javascript
复制
input CreateQuestionWithManyAnswersInput {
    question: CreateQuestionInput!
    answers: [CreateAnswerInput!]!
}

实际的类型是相当无聊的,一个ID,一些字符串,等等。

我已经将管道解析器附加到appsync中的突变,但我不知道如何编写单独的请求和响应映射模板。

到目前为止,我在createQuestion函数中得到了这样的结果:

请求映射模板:

代码语言:javascript
复制
{
    "operation": "PutItem",
    "key": {
        "id": $util.dynamodb.toDynamoDBJson($ctx.args.input.question.id),
    },
    "attributeValues" : $util.dynamodb.toMapValuesJson($ctx.args.input.question)
}

响应映射模板:

代码语言:javascript
复制
$util.qr($ctx.stash.put("question", $util.toJson($ctx.result)))
$util.toJson($ctx.result)

对于创建的answersFunction,我得到:

请求映射模板:

代码语言:javascript
复制
#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)
    }
}

响应映射模板:

代码语言:javascript
复制
$util.qr($ctx.stash.put("answers", $util.toJson($ctx.result).items))
$util.toJson($ctx.result)

中,在映射管道的模板之后,我得到了:

代码语言:javascript
复制
$util.qr($ctx.result.put("question", $ctx.stash.question))
$util.qr($ctx.result.put("answers", $ctx.stash.question.answers))
$util.toJson($ctx.result)

但是,以下情况未能做到这一点:

代码语言:javascript
复制
{
  "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)"
    }
  ]
}

突变型

代码语言:javascript
复制
type Mutation {
    createQuestionWithManyAnswers(input: CreateQuestionWithManyAnswersInput!): QuestionWithManyAnswers
}

在Tinou的帮助下,更新了,我得到了进一步的帮助。基本上,我现在的问题归结为如何在响应映射模板中将存储的内容转换为有效的响应?

我的日志

代码语言:javascript
复制
"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"
    }
}

我的回应是:

代码语言:javascript
复制
{
  "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)"
    }
  ]
}

突变类型:

代码语言:javascript
复制
type Mutation {
    createQuestionWithManyAnswers(input: CreateQuestionWithManyAnswersInput!): QuestionWithManyAnswers
}

QuestionWithManyAnswers类型:

代码语言:javascript
复制
type QuestionWithManyAnswers {
    question: Question!
    answers: [Answer]!
}
EN

回答 1

Stack Overflow用户

发布于 2019-07-15 17:52:54

你能和你正在使用的其他类型的定义一起分享你的突变类型吗?这样我们就可以帮助你复制了吗?模板之前的管道解析器是什么?

尽管如此,我看到了一些问题,首先,您的createQuestion请求映射模板,util调用缺少一个$

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

此外,在createAnswersFunction响应映射模板中,您试图访问json上的items属性。下面这些都不起作用

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

相反,你可能是说

代码语言:javascript
复制
$util.toJson($ctx.result.items)

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

代码语言:javascript
复制
$ctx.result.data.get("Answer-ium2s7hanfgl3dwbamwgloetsi-dev")

一般来说,在开发过程中,我建议您至少在CloudWatch级别启用Error日志。这将为您提供字段级日志信息,即您可以访问调用结果、评估的映射模板、存储内容等等。这对调试很有帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57043793

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档