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

时间:2020-01-29 20:28:18

标签: amazon-web-services aws-lambda aws-appsync

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

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

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

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

预先感谢您<3

1 个答案:

答案 0 :(得分:2)

您可以在请求映射模板中使用#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"
        }
    }
}

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