@@ -239,66 +239,19 @@ When processing events with Lambda, you can return errors to AppSync in three wa
239239
240240=== "Publish events - Class library handler"
241241
242- ```chsarp hl_lines="1 5 9-15 20"
243- using AWS.Lambda.Powertools.EventHandler.AppSyncEvents;
244-
245- public class Function
246- {
247- AppSyncEventsResolver _app;
248-
249- public Function()
250- {
251- _app = new AppSyncEventsResolver();
252- _app.OnPublishAsync("/default/channel", async (payload) =>
253- {
254- // Handle events or
255- // return unchanged payload
256- return payload;
257- });
258- }
259-
260- public async Task<AppSyncEventsResponse> FunctionHandler(AppSyncEventsRequest input, ILambdaContext context)
261- {
262- return await _app.ResolveAsync(input, context);
263- }
264- }
242+ ```csharp hl_lines="1 5 9-15 20"
243+ --8<-- "docs/snippets/appsync-events/GettingStarted.cs:publish_class_library"
265244 ```
266245=== "Publish events - Executable assembly handlers"
267246
268- ```chsarp hl_lines="1 3 5-10 14"
269- using AWS.Lambda.Powertools.EventHandler.AppSyncEvents;
270-
271- var app = new AppSyncEventsResolver();
272-
273- app.OnPublishAsync("/default/channel", async (payload) =>
274- {
275- // Handle events or
276- // return unchanged payload
277- return payload;
278- }
279-
280- async Task<AppSyncEventsResponse> Handler(AppSyncEventsRequest appSyncEvent, ILambdaContext context)
281- {
282- return await app.ResolveAsync(appSyncEvent, context);
283- }
284-
285- await LambdaBootstrapBuilder.Create((Func<AppSyncEventsRequest, ILambdaContext, Task<AppSyncEventsResponse>>)Handler,
286- new DefaultLambdaJsonSerializer())
287- .Build()
288- .RunAsync();
289-
247+ ```csharp hl_lines="1 3 5-10 14"
248+ --8<-- "docs/snippets/appsync-events/GettingStarted.cs:publish_executable_assembly"
290249 ```
291250
292251=== "Subscribe to events"
293252
294253 ```csharp
295- app.OnSubscribe("/default/*", (payload) =>
296- {
297- // Handle subscribe events
298- // return true to allow subscription
299- // return false or throw to reject subscription
300- return true;
301- });
254+ --8<-- "docs/snippets/appsync-events/GettingStarted.cs:subscribe_events"
302255 ```
303256
304257## Advanced
@@ -312,25 +265,7 @@ When an event matches with multiple handlers, the most specific pattern takes pr
312265=== "Wildcard patterns"
313266
314267 ```csharp
315- app.OnPublish("/default/channel1", (payload) =>
316- {
317- // This handler will be called for events on /default/channel1
318- return payload;
319- });
320-
321- app.OnPublish("/default/*", (payload) =>
322- {
323- // This handler will be called for all channels in the default namespace
324- // EXCEPT for /default/channel1 which has a more specific handler
325- return payload;
326- });
327-
328- app.OnPublish("/*", (payload) =>
329- {
330- # This handler will be called for all channels in all namespaces
331- # EXCEPT for those that have more specific handlers
332- return payload;
333- });
268+ --8<-- "docs/snippets/appsync-events/WildcardPatterns.cs:wildcard_patterns"
334269 ```
335270
336271???+ note "Supported wildcard patterns"
@@ -357,29 +292,7 @@ In some scenarios, you might want to process all events for a channel as a batch
357292=== "Aggregated processing"
358293
359294 ```csharp
360- app.OnPublishAggregate("/default/channel", (payload) =>
361- {
362- var evt = new List<AppSyncEvent>();
363-
364- foreach (var item in payload.Events)
365- {
366- if (item.Payload["eventType"].ToString() == "data_2")
367- {
368- pd.Payload["message"] = "Hello from /default/channel2 with data_2";
369- pd.Payload["data"] = new Dictionary<string, object>
370- {
371- { "key", "value" }
372- };
373- }
374-
375- evt.Add(pd);
376- }
377-
378- return new AppSyncEventsResponse
379- {
380- Events = evt
381- };
382- });
295+ --8<-- "docs/snippets/appsync-events/AggregatedProcessing.cs:aggregated_processing"
383296 ```
384297
385298### Handling errors
@@ -393,19 +306,13 @@ When processing items individually with `OnPublish()` and `OnPublishAsync()`, yo
393306=== "Error handling individual items"
394307
395308 ```csharp
396- app.OnPublish("/default/channel", (payload) =>
397- {
398- throw new Exception("My custom exception");
399- });
309+ --8<-- "docs/snippets/appsync-events/ErrorHandling.cs:error_handling_individual"
400310 ```
401311
402312=== "Error handling individual items Async"
403313
404314 ```csharp
405- app.OnPublishAsync("/default/channel", async (payload) =>
406- {
407- throw new Exception("My custom exception");
408- });
315+ --8<-- "docs/snippets/appsync-events/ErrorHandling.cs:error_handling_individual_async"
409316 ```
410317
411318=== "Error handling individual items response"
@@ -434,19 +341,13 @@ When processing batch of items with `OnPublishAggregate()` and `OnPublishAggrega
434341=== "Error handling batch items"
435342
436343 ```csharp
437- app.OnPublishAggregate("/default/channel", (payload) =>
438- {
439- throw new Exception("My custom exception");
440- });
344+ --8<-- "docs/snippets/appsync-events/ErrorHandling.cs:error_handling_batch"
441345 ```
442346
443347=== "Error handling batch items Async"
444348
445349 ```csharp
446- app.OnPublishAggregateAsync("/default/channel", async (payload) =>
447- {
448- throw new Exception("My custom exception");
449- });
350+ --8<-- "docs/snippets/appsync-events/ErrorHandling.cs:error_handling_batch_async"
450351 ```
451352
452353=== "Error handling batch items response"
@@ -469,10 +370,7 @@ You can also reject the entire payload by raising an `UnauthorizedException`. Th
469370=== "Rejecting the entire request"
470371
471372 ```csharp
472- app.OnPublish("/default/channel", (payload) =>
473- {
474- throw new UnauthorizedException("My custom exception");
475- });
373+ --8<-- "docs/snippets/appsync-events/ErrorHandling.cs:unauthorized_exception"
476374 ```
477375
478376### Accessing Lambda context and event
@@ -482,11 +380,7 @@ You can access to the original Lambda event or context for additional informatio
482380=== "Accessing Lambda context"
483381
484382 ```csharp hl_lines="1 3"
485- app.OnPublish("/default/channel", (payload, ctx) =>
486- {
487- payload["functionName"] = ctx.FunctionName;
488- return payload;
489- });
383+ --8<-- "docs/snippets/appsync-events/AdvancedUsage.cs:accessing_lambda_context"
490384 ```
491385
492386## Event Handler workflow
@@ -599,26 +493,7 @@ You can test your event handlers by passing a mocked or actual AppSync Events La
599493=== "Test Publish events"
600494
601495 ```csharp
602- [Fact]
603- public void Should_Return_Unchanged_Payload()
604- {
605- // Arrange
606- var lambdaContext = new TestLambdaContext();
607- var app = new AppSyncEventsResolver();
608-
609- app.OnPublish("/default/channel", payload =>
610- {
611- // Handle channel events
612- return payload;
613- });
614-
615- // Act
616- var result = app.Resolve(_appSyncEvent, lambdaContext);
617-
618- // Assert
619- Assert.Equal("123", result.Events[0].Id);
620- Assert.Equal("test data", result.Events[0].Payload?["data"].ToString());
621- }
496+ --8<-- "docs/snippets/appsync-events/Testing.cs:test_publish_events"
622497 ```
623498
624499=== "Publish event json"
@@ -695,32 +570,5 @@ You can test your event handlers by passing a mocked or actual AppSync Events La
695570=== "Test Subscribe with code payload mock"
696571
697572 ```csharp
698- [Fact]
699- public async Task Should_Authorize_Subscription()
700- {
701- // Arrange
702- var lambdaContext = new TestLambdaContext();
703- var app = new AppSyncEventsResolver();
704-
705- app.OnSubscribeAsync("/default/*", async (info) => true);
706-
707- var subscribeEvent = new AppSyncEventsRequest
708- {
709- Info = new Information
710- {
711- Channel = new Channel
712- {
713- Path = "/default/channel",
714- Segments = ["default", "channel"]
715- },
716- Operation = AppSyncEventsOperation.Subscribe,
717- ChannelNamespace = new ChannelNamespace { Name = "default" }
718- }
719- };
720- // Act
721- var result = await app.ResolveAsync(subscribeEvent, lambdaContext);
722-
723- // Assert
724- Assert.Null(result);
725- }
573+ --8<-- "docs/snippets/appsync-events/Testing.cs:test_subscribe_events"
726574 ```
0 commit comments