适用于多租户的 AWS Appsync GraphQL OnCreate 订阅解析器

问题描述

作为参考,我在 Appsync 中有一个名为 Clients 的表,我编辑了解析器以将用户所属的组附加到他们在名为 userGroups 的字段中创建的客户端对象。

我一直在尝试编写一个解析器来附加到 onCreateClient 订阅,这样如果用户不是存储在 userGroups 中的用户池组的一部分 - 他们在订阅时将不会收到新客户端。我已经在列表和获取中实现了这一点,但到目前为止还没有运气处理 onCreateClient 订阅上的请求和响应模板。这种行为可以通过这种方式实现吗? 我也尝试使用这里的实时示例:https://docs.aws.amazon.com/appsync/latest/devguide/security-authorization-use-cases.html#real-time-data

我在请求和响应映射模板中尝试了许多不同的东西......现在我已经得到了这个,它现在甚至不允许我订阅,给我一个错误:“消息”:“连接失败:{"errors":[{"errorType":"Unauthorized","message":"未授权访问 onCreateClient 类型订阅"}]}"

请求映射:

#set($expressionValues = {})
#foreach($group in $context.identity.claims.get("cognito:groups"))
    #set( $expression = "${expression} contains(ownerGroups,:var$foreach.count )" )
    #set( $val = {})
    #set( $test = $val.put("S",$group))
    #set( $values = $expressionValues.put(":var$foreach.count",$val))
    #if ( $foreach.hasNext )
    #set( $expression = "${expression} OR" )
    #end
#end

{
    "version" : "2017-02-28","operation" : "Scan","limit" : 1,"filter":{
        "expression": "$expression","expressionValues": $utils.toJson($expressionValues)
    }
}

响应映射

#if($ctx.error)
    $utils.appendError($ctx.error.message,$ctx.error.message)
#elseif(! ${context.result}) ##checks if there is a result
    $utils.unauthorized()
#elseif(${context.identity.claims.get("cognito:groups")} != ${context.arguments.ownerGroups})
    $utils.unauthorized()
#else 
    null
#end

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)