Skip to content
This repository was archived by the owner on May 17, 2025. It is now read-only.
This repository was archived by the owner on May 17, 2025. It is now read-only.

Nested attributes do not work when filtering events #272

Open
@michelalbers

Description

@michelalbers

Assume a filter:

filter: (_, _, context) => ({
    update: {
       userId: "foo"
    }
})

Which will be converted to the dynamodb query:

{
      TableName: "graphql_subscriptions",
      IndexName: "TopicIndex",
      ExpressionAttributeNames: {
        "#hashKey": "topic",
        "#filter": "filter",
        "#0": "update.userId",
      },
      ExpressionAttributeValues: {
        ":hashKey": "onUserBadgeCountChange",
        ":0": "foo",
      },
      KeyConditionExpression: "#hashKey = :hashKey",
      FilterExpression:  "(#filter.#0 = :0  OR attribute_not_exists(#filter.#0))",
}

Which is incorrect. As a result the filter will not work and all subscribers will receive the message.

Nested filters have to be written like this:

{
      TableName: "graphql_subscriptions",
      IndexName: "TopicIndex",
      ExpressionAttributeNames: {
        "#hashKey": "topic",
        "#filter": "filter",
        "#0": "update",
        "#1": "userId" // split the key by "."
      },
      ExpressionAttributeValues: {
        ":hashKey": "onUserBadgeCountChange",
        ":0": "foo",
      },
      KeyConditionExpression: "#hashKey = :hashKey",
      FilterExpression:  "(#filter.#0.#1 = :0  OR attribute_not_exists(#filter.#0.#1))", // Join the key back together with #0.#1
}

A very early draft of a solution inside getFilteredSubs.ts looks like this:

for (const [key, value] of Object.entries(flattenPayload)) {
  const aliasNumber = attributeCounter++;
  let subAliasNumbers = [];
  let subAliasNumber = aliasNumber;
  for (const part of key.split('.')) {
    expressionAttributeNames[`#${subAliasNumber}`] = part;
    subAliasNumbers.push(subAliasNumber);
    subAliasNumber++;
  }
  expressionAttributeValues[`:${aliasNumber}`] = value;
  filterExpressions.push(`(#filter.${subAliasNumbers.map(n => `#${n}`).join('.')} = :${aliasNumber} OR attribute_not_exists(#filter.${subAliasNumbers.map(n => `#${n}`).join('.')})`);
}

Unfortunetaly the docs are misleading as well - nested values are clearly described as "working fine" when not working at all.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions