gpt4 book ai didi

graphql - AWS AppSync : pass arguments from parent resolver to children

转载 作者:行者123 更新时间:2023-12-02 10:08:31 27 4
gpt4 key购买 nike

在 AWS AppSync 中,在主查询上发送的参数似乎不会转发到所有子解析器。

type Query {
article(id: String!, consistentRead: Boolean): Article
book(id: String!, consistentRead: Boolean): Book
}

type Article {
title: String!
id: String!
}

type Book {
articleIds: [String]!
articles: [Article]!
id: String!
}

当我打电话时:

query GetBook {
book(id: 123, consistentRead: true) {
articles {
title
}
}
}

获取书籍的第一个查询会收到 $context.arguments 中的 consistentRead 参数,但随后检索文章的查询则不会。 ($context.arguments 为空)

我还尝试了 book 中的 articles(consistentRead: Boolean): [Article]! 但没有运气。

有谁知道 AppSync 是否可以将参数传递给同一请求的所有查询部分?

最佳答案

可以通过响应将参数从父级传递给子级。让我解释一下...

AppSync 在 $context 内有多个容器:

  • 参数
  • 藏起来
  • 来源

argumentsstash 在调用子解析器之前始终会被清除,从这些 Cloudwatch 日志中可以明显看出:

在父执行的最后 - 存在参数存储数据。

{
"errors": [],
"mappingTemplateType": "After Mapping",
"path": "[getLatestDeviceState]",
"resolverArn": "arn:aws:appsync:us-east-1:xxx:apis/yyy/types/Query/fields/getLatestDeviceState",
"context": {
"arguments": {
"device": "ddddd"
},
"prev": {
"result": {
"items": [
{
"version": "849",
"device": "ddddd",
"timestamp": "2019-01-29T12:18:34.504+13:00"
}
]
}
},
"stash": {"testKey": "testValue"},
"outErrors": []
},
"fieldInError": false
}

然后在子解析器的最开始处 - 参数存储始终为空。

{
"errors": [],
"mappingTemplateType": "Before Mapping",
"path": "[getLatestDeviceState, media]",
"resolverArn": "arn:aws:appsync:us-east-1:yyy:apis/xxx/types/DeviceStatePRODConnection/fields/media",
"context": {
"arguments": {},
"source": {
"items": [
{
"version": "849",
"device": "ddddd",
"timestamp": "2019-01-29T12:18:34.504+13:00"
}
]
},
"stash": {},
"outErrors": []
},
"fieldInError": false
}

解决方法 1 - 从之前的结果中获取参数。

在上面的示例中,device 始终存在于父解析器的响应中,因此我插入了

#set($device = $util.defaultIfNullOrBlank($ctx.args.device, $ctx.source.items[0].device))

进入子解析器的请求映射模板。它将尝试从参数中获取所需的 ID,然后返回到之前的结果。

解决方法 2 - 将参数添加到父响应

修改您的父解析器响应模板以包含参数:

{
"items": $utils.toJson($context.result.items),
"device": "${ctx.args.device}"
}

然后按照与第一个解决方法相同的方式在子级的请求映射模板中检索它。

关于graphql - AWS AppSync : pass arguments from parent resolver to children,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52512011/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com