Skip to content

Commit 34a4654

Browse files
authored
docs(event-handler): add examples on how to drop messages (#3924)
1 parent 27a6470 commit 34a4654

File tree

4 files changed

+49
-9
lines changed

4 files changed

+49
-9
lines changed

docs/features/event-handler/appsync-events.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,15 +167,24 @@ This is useful when you want to for example:
167167

168168
You can enable this with the `aggregate` parameter:
169169

170-
!!! note "Aggregate Processing"
171-
When enabling `aggregate`, your handler receives a list of all events, requiring you to manage the response format. Ensure your response includes results for each event in the expected [AppSync Request and Response Format](#appsync-request-and-response-format).
172-
173170
=== "Aggregated processing"
174171

175172
```typescript hl_lines="17 32 34"
176173
--8<-- "examples/snippets/event-handler/appsync-events/aggregatedProcessing.ts"
177174
```
178175

176+
When enabling `aggregate`, your handler receives a list of all the events, requiring you to manage the response format. Ensure your response includes results for each event in the expected [AppSync Request and Response Format](#appsync-request-and-response-format).
177+
178+
If you want to omit one or more events from the response, you can do so by excluding them from the returned array. Likewise, if you want to discard the entire batch and prevent subscribers from receiving it, you can return an empty array.
179+
180+
=== "Aggregated processing with partial results"
181+
182+
```typescript hl_lines="17 19"
183+
--8<-- "examples/snippets/event-handler/appsync-events/aggregatedProcessingWithPartialResults.ts"
184+
```
185+
186+
1. You can also return an empty array `[]` to discard the entire batch and prevent subscribers from receiving it.
187+
179188
### Handling errors
180189

181190
You can filter or reject events by throwing exceptions in your resolvers or by formatting the payload according to the expected response structure. This instructs AppSync not to propagate that specific message, so subscribers will not receive it.
@@ -237,7 +246,7 @@ You can also do content-based authorization for channel by throwing an `Unauthor
237246

238247
=== "UnauthorizedException"
239248

240-
```typescript hl_lines="3 14 20"
249+
```typescript hl_lines="3 14 25-27"
241250
--8<-- "examples/snippets/event-handler/appsync-events/unauthorizedException.ts"
242251
```
243252

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { AppSyncEventsResolver } from '@aws-lambda-powertools/event-handler/appsync-events';
2+
import type { AppSyncEventsPublishEvent } from '@aws-lambda-powertools/event-handler/types';
3+
import type { Context } from 'aws-lambda';
4+
5+
const app = new AppSyncEventsResolver();
6+
7+
app.onPublish(
8+
'/default/foo/*',
9+
async (events) => {
10+
const payloadsToReturn: AppSyncEventsPublishEvent['events'] = [];
11+
12+
for (const event of events) {
13+
if (event.payload.includes('foo')) continue;
14+
payloadsToReturn.push(event);
15+
}
16+
17+
return payloadsToReturn; // (1)!
18+
},
19+
{ aggregate: true }
20+
);
21+
22+
export const handler = async (event: unknown, context: Context) =>
23+
app.resolve(event, context);

examples/snippets/event-handler/appsync-events/unauthorizedException.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,18 @@ app.onPublish('/*', () => {
1414
throw new UnauthorizedException('You can only publish to /default/foo');
1515
});
1616

17-
app.onSubscribe('/default/foo', () => true);
17+
app.onSubscribe('/private/*', async (info) => {
18+
const userGroups =
19+
info.identity?.groups && Array.isArray(info.identity?.groups)
20+
? info.identity?.groups
21+
: [];
22+
const channelGroup = 'premium-users';
1823

19-
app.onSubscribe('/*', () => {
20-
throw new UnauthorizedException('You can only subscribe to /default/foo');
24+
if (!userGroups.includes(channelGroup)) {
25+
throw new UnauthorizedException(
26+
`Subscription requires ${channelGroup} group membership`
27+
);
28+
}
2129
});
2230

2331
export const handler = async (event: unknown, context: Context) =>

packages/event-handler/src/types/appsync-events.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,12 @@ type RouteOptions<T extends boolean | undefined = false> = {
168168
*/
169169
type AppSyncEventsEvent = {
170170
/**
171-
* The `identity` field is marked as `unknown` because it varies based on the authentication type used in AppSync.
171+
* The `identity` field varies based on the authentication type used for the AppSync API.
172172
* When using an API key, it will be `null`. When using IAM, it will contain the AWS credentials of the user. When using Cognito,
173173
* it will contain the Cognito user pool information. When using a Lambda authorizer, it will contain the information returned
174174
* by the authorizer.
175175
*/
176-
identity: unknown;
176+
identity: null | Record<string, unknown>;
177177
result: null;
178178
request: {
179179
headers: Record<string, string>;

0 commit comments

Comments
 (0)