Skip to content

Commit 4493165

Browse files
Refactoring examples + docs
1 parent 84c3590 commit 4493165

6 files changed

+100
-16
lines changed

docs/core/event_handler/appsync.md

+19-9
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ You can use `append_context` when you want to share data between your App and Ro
290290

291291
### Batch processing
292292

293-
We support Appsync's batching mechanism for Lambda Resolvers. To handle multiple events in a batch and prevent multiple lambda executions, configure your Appsync to group events and use the `@batch_resolver` or `@async_batch_resolver` decorators.
293+
We support AWS Appsync's batching mechanism for Lambda Resolvers. It prevents multiple Lambda executions by grouping events and using the `@batch_resolver` or `@async_batch_resolver` decorators to resolve the entire batch.
294294

295295
???+ info
296296
If you want to understand more how to configure batch processing for the AppSync, please follow this [guide](https://aws.amazon.com/blogs/mobile/introducing-configurable-batching-size-for-aws-appsync-lambda-resolvers/){target="_blank"}.
@@ -300,28 +300,38 @@ We support Appsync's batching mechanism for Lambda Resolvers. To handle multiple
300300
--8<-- "examples/event_handler_graphql/src/getting_started_with_batch_resolver.py"
301301
```
302302

303+
1. The entire batch is sent to the resolver, and you need to iterate through it to process all records.
304+
303305
=== "getting_started_with_batch_resolver_payload.json"
304306
```json hl_lines="4 16 21 29 41 46"
305307
--8<-- "examples/event_handler_graphql/src/getting_started_with_batch_resolver_payload.json"
306308
```
307309

308-
#### Handling exceptions
310+
#### Processing Batch items individually
309311

310-
By default, records that fail during Lambda execution return `None` to ensure the entire batch doesn't fail due to processing errors. However, you have the option to actively return exceptions for debugging or troubleshooting.
312+
You can process each item in the batch individually, and we can handle exceptions for you. However, it's important to note that utilizing this method may increase the execution time of your Lambda function.
311313

312314
???+ tip
313315
For better error handling, you may need to configure response mapping templates and specify error keys. Explore more on returning individual errors [here](https://docs.aws.amazon.com/appsync/latest/devguide/tutorial-lambda-resolvers.html#advanced-use-case-batching){target="_blank"}.
314316

315-
=== "enable_exceptions_batch_resolver.py"
316-
```python hl_lines="3 7 21"
317-
--8<-- "examples/event_handler_graphql/src/enable_exceptions_batch_resolver.py"
317+
=== "getting_started_with_batch_resolver_individual.py"
318+
```python hl_lines="3 7 17"
319+
--8<-- "examples/event_handler_graphql/src/getting_started_with_batch_resolver_individual.py"
320+
```
321+
322+
1. You need to disable the aggregated event by using `aggregate` flag.
323+
The resolver receives and processes each record one at a time.
324+
325+
=== "getting_started_with_batch_resolver_handling_error.py"
326+
```python hl_lines="3 7 17"
327+
--8<-- "examples/event_handler_graphql/src/getting_started_with_batch_resolver_handling_error.py"
318328
```
319329

320-
1. You can enable the exceptions by setting `raise_on_error` to True.
330+
1. You can enable enable the error handling by using `raise_on_error` flag.
321331

322-
=== "enable_exceptions_batch_resolver_payload.json"
332+
=== "getting_started_with_batch_resolver_payload.json"
323333
```json hl_lines="4 16 21 29 41 46"
324-
--8<-- "examples/event_handler_graphql/src/enable_exceptions_batch_resolver_payload.json"
334+
--8<-- "examples/event_handler_graphql/src/getting_started_with_batch_resolver_payload.json"
325335
```
326336

327337
#### Async

examples/event_handler_graphql/src/getting_started_with_batch_async_resolver.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
from typing import Dict, Optional
1+
from typing import Any, List, Optional
22

3+
from aws_lambda_powertools import Logger
34
from aws_lambda_powertools.event_handler import AppSyncResolver
45
from aws_lambda_powertools.utilities.data_classes import AppSyncResolverEvent
56
from aws_lambda_powertools.utilities.typing import LambdaContext
67

8+
logger = Logger()
79
app = AppSyncResolver()
810

911

@@ -15,8 +17,18 @@
1517

1618

1719
@app.async_batch_resolver(type_name="Query", field_name="relatedPosts")
18-
async def related_posts(event: AppSyncResolverEvent, post_id: str) -> Optional[Dict]:
19-
return posts_related.get(post_id, None)
20+
async def related_posts(event: List[AppSyncResolverEvent]) -> Optional[List[Any]]:
21+
results = []
22+
23+
for record in event: # (1)!
24+
post_id = record.arguments.get("post_id")
25+
try:
26+
results.append(posts_related[post_id] if post_id else None)
27+
# Add other logic here
28+
except Exception:
29+
logger.error("Error processing record", post_id=post_id)
30+
31+
return results
2032

2133

2234
def lambda_handler(event, context: LambdaContext) -> dict:

examples/event_handler_graphql/src/getting_started_with_batch_resolver.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
from typing import Dict, Optional
1+
from typing import Any, List
22

3+
from aws_lambda_powertools import Logger
34
from aws_lambda_powertools.event_handler import AppSyncResolver
45
from aws_lambda_powertools.utilities.data_classes import AppSyncResolverEvent
56
from aws_lambda_powertools.utilities.typing import LambdaContext
67

8+
logger = Logger()
79
app = AppSyncResolver()
810

911

@@ -15,8 +17,18 @@
1517

1618

1719
@app.batch_resolver(type_name="Query", field_name="relatedPosts")
18-
def related_posts(event: AppSyncResolverEvent, post_id: str) -> Optional[Dict]:
19-
return posts_related.get(post_id, None)
20+
def related_posts(event: List[AppSyncResolverEvent]) -> List[Any]:
21+
results = []
22+
23+
for record in event: # (1)!
24+
post_id = record.arguments.get("post_id")
25+
try:
26+
results.append(posts_related[post_id] if post_id else None)
27+
# Add other logic here
28+
except Exception:
29+
logger.error("Error processing record", post_id=post_id)
30+
31+
return results
2032

2133

2234
def lambda_handler(event, context: LambdaContext) -> dict:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from typing import Any, Dict
2+
3+
from aws_lambda_powertools import Logger
4+
from aws_lambda_powertools.event_handler import AppSyncResolver
5+
from aws_lambda_powertools.utilities.data_classes import AppSyncResolverEvent
6+
from aws_lambda_powertools.utilities.typing import LambdaContext
7+
8+
logger = Logger()
9+
app = AppSyncResolver()
10+
11+
12+
posts_related = {
13+
"1": {"title": "post1"},
14+
"2": {"title": "post2"},
15+
"3": {"title": "post3"},
16+
}
17+
18+
19+
@app.batch_resolver(type_name="Query", field_name="relatedPosts", aggregate=False, raise_on_error=True) # (1)!
20+
def related_posts(event: AppSyncResolverEvent, post_id: str = "") -> Dict[str, Any]:
21+
return posts_related[post_id]
22+
23+
24+
def lambda_handler(event, context: LambdaContext) -> dict:
25+
return app.resolve(event, context)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from typing import Any, Dict
2+
3+
from aws_lambda_powertools import Logger
4+
from aws_lambda_powertools.event_handler import AppSyncResolver
5+
from aws_lambda_powertools.utilities.data_classes import AppSyncResolverEvent
6+
from aws_lambda_powertools.utilities.typing import LambdaContext
7+
8+
logger = Logger()
9+
app = AppSyncResolver()
10+
11+
12+
posts_related = {
13+
"1": {"title": "post1"},
14+
"2": {"title": "post2"},
15+
"3": {"title": "post3"},
16+
}
17+
18+
19+
@app.batch_resolver(type_name="Query", field_name="relatedPosts", aggregate=False) # (1)!
20+
def related_posts(event: AppSyncResolverEvent, post_id: str = "") -> Dict[str, Any]:
21+
return posts_related[post_id]
22+
23+
24+
def lambda_handler(event, context: LambdaContext) -> dict:
25+
return app.resolve(event, context)

examples/event_handler_graphql/src/getting_started_with_batch_resolver_payload.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
},
2727
{
2828
"arguments":{
29-
"post_id":"10"
29+
"post_id":"2"
3030
},
3131
"identity":"None",
3232
"source":"None",

0 commit comments

Comments
 (0)