我可以有条件地调用 AWS Appsync 解析器中的 lambda 函数吗?

muf*_*asa 4 amazon-web-services aws-lambda aws-appsync

paymentStatus我已将 Appsync 管道解析器附加到我的对象中调用的字段Organisation。这个想法是,如果组织的最后一个发薪日已经过去,我想使用 Lambda 函数从外部 API 获取付款状态。如果发薪日尚未过去,我不想调用该函数,而只是返回“OK”。

有什么方法可以有条件地调用 Lambda 函数吗?像这样的东西:

#if ($ctx.source.payday < $util.time.nowEpochSeconds()) 
    {
        "version": "2017-02-28",
        "operation": "Invoke",
        "payload": {
            "arguments": {
                "orgID": "$ctx.source.id"
            }
        }
    }
#end
Run Code Online (Sandbox Code Playgroud)

operation如果我运行此命令,Appsync 会在不满足条件时抱怨属性丢失。我还注意到,condition用于查询的属性不适用于 Lambda 数据源。

预先感谢您<3

Tin*_*nou 6

您可以使用#return请求映射模板内的指令提前从模板返回,从而有效地从单元解析器提前返回。

您的请求映射模板可能如下所示:

#if ($ctx.source.payday >= $util.time.nowEpochSeconds()) 
  #set($result = "OK")
  #return($result)
#end

{
    "version": "2017-02-28",
    "operation": "Invoke",
    "payload": {
        "arguments": {
            "orgID": "$ctx.source.id"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

有关更多详细信息,您可以阅读https://docs.aws.amazon.com/appsync/latest/devguide/resolver-util-reference.html#aws-appsync-directives