Skip to content

Enhanced Filtering for AppSync Subscriptions failing when number of unique fields is greater than 5

0

I am trying to implement enhanced server-side subscription filtering for some mutations on my AppSync application. I understand that there are some limitations with this feature, namely:

  • AWS AppSync supports up to five levels of nesting
  • In the filters object, there can be a maximum of five unique fieldName items per filter. This means that you can combine a maximum of five individual fieldName objects using AND logic.
  • Each filterGroup can have a maximum of 10 filters. This means that you can combine a maximum of 10 filters using OR logic.

These restrictions are according to https://docs.aws.amazon.com/appsync/latest/devguide/aws-appsync-real-time-enhanced-filtering.html#aws-appsync-real-time-enhanced-filtering-additional-restrictions https://docs.aws.amazon.com/appsync/latest/devguide/extensions.html , under the Exceptions section

Here are the relevant GQL entities related to the mutation/subscription in question

type Thing {
  start: AWSDateTime!
  end: AWSDateTime!
  parent: Parent
}

type Parent {
  type: string!
}

extend type Mutation {
  editThing(input: ThingDeltaInput!): Thing!
}

extend type Subscription {
  editedThing(input: SubscriptionFilterInput!): ThingDeltaResponse @aws_subscribe(mutations: ["editThing"])
}

type ThingDeltaResponse {
  before: Thing!
  after: Thing!
}

input SubscriptionFilterInput {
  fromDate: AWSDateTime!
  toDate: AWSDateTime!
  parentTypes: [String!]
}

To summarize, I am invoking a mutation to update a data entity, then returning the entity's state before and after the update in two fields before and after. I have written an Appsync_JS resolver following the Enhanced Filtering guide, using the extensions.setSubscriptionFilter and the util.transform.toSubscriptionFilter methods to set a subscriber's filter criteria. An example resulting filter is below

{
  "filterGroup": [
    {
      "filters": [
        {
          "fieldName": "before.start",
          "operator": "le",
          "value": "2024-09-03T22:00:00.000Z"
        },
        {
          "fieldName": "before.end",
          "operator": "ge",
          "value": "2024-09-03T02:00:00.000Z",
        },
        {
          "fieldName": "before.parent.type",
          "operator": "in",
          "value": [
            "SOME_TYPE"
          ]
        }
      ]
    },
    {
      "filters": [
        {
          "fieldName": "after.start",
          "operator": "le",
          "value": "2024-09-03T22:00:00.000Z"
        },
        {
          "fieldName": "after.end",
          "operator": "ge",
          "value": "2024-09-03T02:00:00.000Z"
        },
        {
          "fieldName": "after.parent.type",
          "operator": "in",
          "value": [
            "SOME_TYPE"
          ]
        }
      ]
    }
  ]
}

The desire here is to push the subscription only if either the before or after meet the filter criteria. Unfortunately trying to set this filter will result in this error when a client subscribes (tested through the AppSync Management console)

{
    "errors": [
        {
            "message": "Connection failed: {\"errors\":[{\"message\":\"Filters exceed maximum attributes limit 5 for subscription.\",\"errorCode\":400}]}"
        }
    ]
}

This is confusing to me because each filter object only has three attributes each. AppSync does, however, allow it if I were to change one of the unique fields to one of the other ones listed, bringing my number of unique fields down to 5 but my total still at 6. So it seems to me that the second restriction I previously mentioned is misleading, as AppSync is not allowing me to input more than 5 unique fields anywhere in the filter, not just a single filter object for AND logic. I'm not sure if I'm misreading anything here and if this is intended behavior, or a bug. Thanks for reading the long post and any help would be appreciated!

asked a year ago281 views
2 Answers
2
Accepted Answer

Hi, I got a confirmation from AppSync development team that the limit of 5 unique fieldName is per subscription field and not per filter. The team has confirmed that they will will update the documentation to reflect this. However we cannot comment on thr ETA for the changes to reflect.

AWS
SUPPORT ENGINEER
answered a year ago
EXPERT
reviewed a year ago
  • Thanks for reaching out for the clarification!

0

Hello,

I tried to relicate this with a sample schema :

enum Priority {
	none
	lowest
	low
	medium
	high
	highest
}

type Ticket {
	id: ID
	createdAt: AWSDateTime
	content: String
	severity: Int
	priority: Priority
	category: String
	group: String
	status: String
}

input TicketInput {
	content: String
	severity: Int
	priority: Priority
	category: String
	group: String
}

type Mutation {
	createTicket(input: TicketInput): Ticket
}

type Query {
	getTicket(id: ID!): Ticket
}

type Subscription {
	onSpecialTicketCreated: Ticket
		@aws_subscribe(mutations: ["createTicket"])
	onGroupTicketCreated(group: String!): Ticket
		@aws_subscribe(mutations: ["createTicket"])
}

Added the resolver code as :

import { util, extensions } from '@aws-appsync/utils';

export function request(ctx) {
	// simplfy return null for the payload
	return { payload: null };
}

export function response(ctx) {
	const filter = {
		or: [
			{ severity: { ge: 7 }, category: {eq : 'AWS AppSync'}, priority: { in: ['high', 'medium', 'none', 'lowest', 'low','highest'] } },
			{ category: { eq: 'security' }, priority: { in: ['high', 'medium', 'none', 'lowest', 'low','highest'] } },
		],
	};
	extensions.setSubscriptionFilter(util.transform.toSubscriptionFilter(filter));

  // important: return null in the response
	return null;
}

which is similar to your setup however I was not able to reproduce this. Can you please share the number of items in the 'in' operator? The limit is 5

Also, this might require account specific investigation, hence you may consider reaching out to AWS Support, directly.

AWS
SUPPORT ENGINEER
answered a year ago
  • Hello, thanks for the comment! I think my issue isn't with the in operator, but moreso a five field requirement I am confused about. My examples all used only one element in the in operator. It's possible I'll need to reach out to AWS directly. In your example, you are using 3 unique fields in your overall filter (severity, category, and priority). There should be no issue with that, but I think I get an error when there are more than 5 unique fields even when they are not directly AND'd. So with your example, each object in the OR array would need to have 3 fields to filter on each, and each one of those fields should be unique, so no duplicates.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.