aws / aws-appsync-community

The AWS AppSync community
https://aws.amazon.com/appsync
Apache License 2.0
506 stars 32 forks source link

Query Permission Tables to apply enhanced subscription filter. #238

Closed LeoReubelt closed 2 years ago

LeoReubelt commented 2 years ago

I am working on a messaging app with the following objects...

type Message
    @model
    @key(name: "byDate", fields: ["channelId", "createdAt"], queryField: "getMessagesByDate") {
    id: ID!
    channelId: ID!
    senderId: ID!
    createdAt: AWSDateTime!
    text: String
}
type UserChannelPermission
    @model
    @key(fields: ["userId"]) {
    userId: ID!
    channelIds: [String!]!
}

I create messages using the following...

createNewMessage(id: ID!, channelId: ID!, senderId: ID!, text: String): Message

And I have the following subscription...

onCreateMessageForMe: Message @aws_subscribe(mutations: ["createNewMessage"])

In the resolver for that onCreateMessageForMe, I am ttrying to query the UserChannelPermission table so I can check if the User is in the channel and filter accordingly. So I set the data source to UserChannelPermissionTable, and set the request mapping to....

{
    "version": "2018-05-29",
    ##"payload": $util.toJson($context.result),
    "operation" : "GetItem",
    "key" : {
        "userId" : $util.dynamodb.toDynamoDBJson($ctx.identity.username),
    },
}

And in my response mapping, I put something like this...

$extensions.setSubscriptionFilter({
    "filterGroup": [
        {
           "filters" : [
                {
                    "fieldName" : "channelId",
                    "operator" : "in",
                    "value" : <get channel ids from query in request mapping>,
                }
           ]
        }
    ]
})

$util.toJson($context.result)

The filter works if I just hard code in a value. But beyond that I can't get it to work.