AWS AppSync 嵌套解析器 - 如何重用来自父级的参数

发布于 2025-01-11 21:45:30 字数 1529 浏览 1 评论 0原文

嘿哟。我有一个 AppSync 解析器,其中有一个附加到解析器的字段。查询接受的参数与内部解析器所需的参数相同。为了简洁起见,我想从上下文中将其传递下来,而不必指定它。解析器的数据源是 dynamoDB 表 假设架构看起来像

type Query {
   getThings(key: String!): AResult!
}

type AResult {
   getOtherThings(key: String!): String!
}

可以构建这样的查询,

query Query {
  getThings(key: "123") {
    getOtherThings(key: "123")
  }
}

这是笨拙且多余的。理想情况下,我只想创建一个如下所示的查询

query Query {
  getThings(key: "123") {
    getOtherThings
  }
}

,并且解析器可以从请求的上下文中提取key并重用它。 getOtherThings 解析器的请求模板如下所示:

{
    "version": "2017-02-28",
    "operation": "Query",
    "query": {
        "expression" : "key = :key",
        "expressionValues" : {
            ":key" : $util.dynamodb.toDynamoDBJson($context.arguments.key)
        }
    }
}

但是 $context.guments.keynull$context.args.key$ctx.args.key$ctx.arguments.key 也是如此。如果我在执行 getThings 时检查请求的日志,我可以看到预期的参数:

{
    "logType": "RequestMapping",
    "path": [
        "getThings"
    ],
    "fieldName": "getThings",
    "context": {
        "arguments": {
            "key": "123"
        },
        "stash": {},
        "outErrors": []
    },
    "fieldInError": false,
    "errors": [],
    "parentType": "Query"
}

因此我推测父解析器 (getThings) 及其父解析器之间的上下文不会持续存在子解析器 (getOtherThings),但我无法从日志中看出这一点。

这可能吗?我正在搜索 AWS 日志

Heyo. I've got an AppSync resolver with a field that is attached to a resolver. The query accepts an argument that is the same argument the inner resolver would need. For the sake of terseness, I'd like to just pass it down from context instead of having to specify it. The datasource for the resolver is a dynamoDB table
Say the schema looks like

type Query {
   getThings(key: String!): AResult!
}

type AResult {
   getOtherThings(key: String!): String!
}

I could construct a query as such

query Query {
  getThings(key: "123") {
    getOtherThings(key: "123")
  }
}

Which is clumsy and redundant. Ideally, I'd just want to create a query that looks like

query Query {
  getThings(key: "123") {
    getOtherThings
  }
}

And the resolver can pull key from the context of the request and reuse it.
The request template for getOtherThings resolver looks like:

{
    "version": "2017-02-28",
    "operation": "Query",
    "query": {
        "expression" : "key = :key",
        "expressionValues" : {
            ":key" : $util.dynamodb.toDynamoDBJson($context.arguments.key)
        }
    }
}

But $context.guments.key is null. As is $context.args.key and $ctx.args.key and $ctx.arguments.key. If I examine the logs from the request when executing getThings I can see the expected arguments:

{
    "logType": "RequestMapping",
    "path": [
        "getThings"
    ],
    "fieldName": "getThings",
    "context": {
        "arguments": {
            "key": "123"
        },
        "stash": {},
        "outErrors": []
    },
    "fieldInError": false,
    "errors": [],
    "parentType": "Query"
}

So I surmise that the context does not persist between the parent resolver (getThings) and its child resolver (getOtherThings), but I can't make this out from the logs.

Is this even possible - I'm coming up dry on searching through AWS logs

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜司空 2025-01-18 21:45:30

答案就在 ctx.source 中。 ctx.source 是父字段的映射,因此我可以从那里获取它。

{
    "logType": "RequestMapping",
    "path": [
        "getThings"
    ], 
    "source": {
       "key":"123"
    },
    "fieldName": "getThings",
    "context": {
        "arguments": {
            "key": "123"
        },
        "stash": {},
        "outErrors": []
    },
    "fieldInError": false,
    "errors": [],
    "parentType": "Query"
}

The answer lies in ctx.source. ctx.source is a map of the parent field, so I can grab it from there.

{
    "logType": "RequestMapping",
    "path": [
        "getThings"
    ], 
    "source": {
       "key":"123"
    },
    "fieldName": "getThings",
    "context": {
        "arguments": {
            "key": "123"
        },
        "stash": {},
        "outErrors": []
    },
    "fieldInError": false,
    "errors": [],
    "parentType": "Query"
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文