GraphQL tip: Have the subscription types contain all filterable fields on the top-level.

For example, a Todo event that should allow filtering by user, group, severity, and id, should have these as well as the item itself:

type TodoEvent {
  userId: ID!
  groupId: ID!
  todoId: ID!
  severity: Severity!
  todo: Todo
}

Originally, it was the only way for filtering in AppSync, but that changed when it started supporting filters on nested fields. So, why it's still a best practice?

The primary reason is deletion: this structure makes it easy to support sending an event when the item is deleted and all the filters will work.

Then an additional benefit is when there is a filter that is not present in the entity itself. For example, if a Todo item's userId can be changed it is useful to notify the original owner. In that case, a prevUserId field can be added to the subscription allowing users to subscribe to events when an item is removed from them.

September 25, 2024

Free PDF guide

Sign up to our newsletter and download the "Git Tips and Tricks" guide.