Skip to content
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 opened this issue Jun 14, 2022 · 0 comments
Open

Nested attributes do not work when filtering events #272

michelalbers opened this issue Jun 14, 2022 · 0 comments

Comments

@michelalbers
Copy link

michelalbers commented Jun 14, 2022

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant