Skip to content

Commit 92db4a2

Browse files
Adding examples
1 parent 2428ebd commit 92db4a2

File tree

6 files changed

+102
-60
lines changed

6 files changed

+102
-60
lines changed

aws_lambda_powertools/utilities/data_classes/appsync_resolver_event.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,9 @@ def identity(self) -> Union[None, AppSyncIdentityIAM, AppSyncIdentityCognito]:
184184
return get_identity_object(self.get("identity"))
185185

186186
@property
187-
def source(self) -> Optional[Dict[str, Any]]:
187+
def source(self) -> Dict[str, Any]:
188188
"""A map that contains the resolution of the parent field."""
189-
return self.get("source")
189+
return self.get("source") or {}
190190

191191
@property
192192
def request_headers(self) -> Dict[str, str]:

docs/core/event_handler/appsync.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,11 @@ In this mode, you must return results in the same order of your batch items, so
383383
--8<-- "examples/event_handler_graphql/src/getting_started_with_batch_resolver_payload.json"
384384
```
385385

386+
=== "getting_started_with_batch_query.graphql"
387+
```typescript hl_lines="4 16 21 29 41 46"
388+
--8<-- "examples/event_handler_graphql/src/getting_started_with_batch_query.graphql"
389+
```
390+
386391
#### Processing items individually
387392

388393
```mermaid
@@ -453,6 +458,11 @@ In this mode, we will:
453458
--8<-- "examples/event_handler_graphql/src/getting_started_with_batch_resolver_payload.json"
454459
```
455460

461+
=== "getting_started_with_batch_query.graphql"
462+
```typescript hl_lines="4 16 21 29 41 46"
463+
--8<-- "examples/event_handler_graphql/src/getting_started_with_batch_query.graphql"
464+
```
465+
456466
##### Raise on error
457467

458468
```mermaid
@@ -521,6 +531,11 @@ This is useful when you want to stop processing immediately in the event of an u
521531
--8<-- "examples/event_handler_graphql/src/getting_started_with_batch_resolver_payload.json"
522532
```
523533

534+
=== "getting_started_with_batch_query.graphql"
535+
```typescript hl_lines="4 16 21 29 41 46"
536+
--8<-- "examples/event_handler_graphql/src/getting_started_with_batch_query.graphql"
537+
```
538+
524539
#### Async
525540

526541
Choose the asynchronous batch processor when your objective is to leverage concurrency capabilities without the necessity of preserving records in a specific order.
@@ -538,6 +553,11 @@ Choose the asynchronous batch processor when your objective is to leverage concu
538553
--8<-- "examples/event_handler_graphql/src/getting_started_with_batch_async_resolver_payload.json"
539554
```
540555

556+
=== "getting_started_with_batch_query.graphql"
557+
```typescript hl_lines="4 16 21 29 41 46"
558+
--8<-- "examples/event_handler_graphql/src/getting_started_with_batch_query.graphql"
559+
```
560+
541561
## Testing your code
542562

543563
You can test your resolvers by passing a mocked or actual AppSync Lambda event that you're expecting.

examples/event_handler_graphql/src/getting_started_with_batch_async_resolver.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,35 @@
1-
from typing import Any, Dict, List
1+
from __future__ import annotations
2+
3+
from typing import Any
24

35
from aws_lambda_powertools.event_handler import AppSyncResolver
46
from aws_lambda_powertools.utilities.data_classes import AppSyncResolverEvent
57
from aws_lambda_powertools.utilities.typing import LambdaContext
68

79
app = AppSyncResolver()
810

11+
# mimic DB data for simplicity
912
posts_related = {
1013
"1": {"title": "post1"},
1114
"2": {"title": "post2"},
1215
"3": {"title": "post3"},
1316
}
1417

1518

16-
def search_batch_posts(posts: List) -> Dict[str, Any]:
19+
def search_batch_posts(posts: list) -> dict[str, Any]:
1720
return {post_id: posts_related.get(post_id) for post_id in posts}
1821

1922

2023
@app.async_batch_resolver(type_name="Query", field_name="relatedPosts")
21-
async def related_posts(event: List[AppSyncResolverEvent]) -> List[Any]:
24+
async def related_posts(event: list[AppSyncResolverEvent]) -> list[Any]: # (1)!
2225
# Extract all post_ids in order
23-
post_ids = [record.arguments.get("post_id") for record in event]
26+
post_ids: list = [record.source.get("post_id") for record in event] # (2)!
2427

2528
# Get unique post_ids while preserving order
2629
unique_post_ids = list(dict.fromkeys(post_ids))
2730

2831
# Fetch posts in a single batch operation
29-
fetched_posts: Dict = search_batch_posts(unique_post_ids)
32+
fetched_posts = search_batch_posts(unique_post_ids)
3033

3134
# Return results in original order
3235
return [fetched_posts.get(post_id) for post_id in post_ids]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
query MyQuery {
2+
getPost(post_id: "2") {
3+
relatedPosts {
4+
post_id
5+
author
6+
relatedPosts {
7+
post_id
8+
author
9+
}
10+
}
11+
}
12+
}

examples/event_handler_graphql/src/getting_started_with_batch_resolver.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ def search_batch_posts(posts: list) -> dict[str, Any]:
2323
@app.batch_resolver(type_name="Query", field_name="relatedPosts")
2424
def related_posts(event: list[AppSyncResolverEvent]) -> list[Any]: # (1)!
2525
# Extract all post_ids in order
26-
post_ids = [record.arguments.get("post_id") for record in event] # (2)!
26+
post_ids: list = [record.source.get("post_id") for record in event] # (2)!
2727

2828
# Get unique post_ids while preserving order
29-
unique_post_ids: list[str] = list(dict.fromkeys(post_ids))
29+
unique_post_ids = list(dict.fromkeys(post_ids))
3030

3131
# Fetch posts in a single batch operation
3232
fetched_posts = search_batch_posts(unique_post_ids)
Lines changed: 58 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,59 @@
11
[
2-
{
3-
"arguments":{
4-
"post_id":"1"
5-
},
6-
"identity":"None",
7-
"source":"None",
8-
"request":{
9-
"headers":{
10-
"x-forwarded-for":"18.68.31.36",
11-
"sec-ch-ua-mobile":"?0"
12-
}
13-
},
14-
"prev":"None",
15-
"info":{
16-
"fieldName":"relatedPosts",
17-
"selectionSetList":[
18-
"relatedPosts"
19-
],
20-
"selectionSetGraphQL":"{\n relatedPosts {\n downs\n content\n relatedPosts {\n ups\n downs\n relatedPosts {\n content\n downs\n }\n }\n }\n}",
21-
"parentTypeName":"Query",
22-
"variables":{
23-
24-
}
25-
}
26-
},
27-
{
28-
"arguments":{
29-
"post_id":"2"
30-
},
31-
"identity":"None",
32-
"source":"None",
33-
"request":{
34-
"headers":{
35-
"x-forwarded-for":"18.68.31.36",
36-
"sec-ch-ua-mobile":"?0"
37-
}
38-
},
39-
"prev":"None",
40-
"info":{
41-
"fieldName":"relatedPosts",
42-
"selectionSetList":[
43-
"relatedPosts"
44-
],
45-
"selectionSetGraphQL":"{\n relatedPosts {\n downs\n content\n relatedPosts {\n ups\n downs\n relatedPosts {\n content\n downs\n }\n }\n }\n}",
46-
"parentTypeName":"Query",
47-
"variables":{
48-
49-
}
50-
}
51-
}
52-
]
2+
{
3+
"arguments":{},
4+
"identity":"None",
5+
"source":{
6+
"post_id":"1",
7+
"author":"Author1"
8+
},
9+
"prev":"None",
10+
"info":{
11+
"selectionSetList":[
12+
"post_id",
13+
"author"
14+
],
15+
"selectionSetGraphQL":"{\n post_id\n author\n}",
16+
"fieldName":"relatedPosts",
17+
"parentTypeName":"Post",
18+
"variables":{}
19+
}
20+
},
21+
{
22+
"arguments":{},
23+
"identity":"None",
24+
"source":{
25+
"post_id":"2",
26+
"author":"Author2"
27+
},
28+
"prev":"None",
29+
"info":{
30+
"selectionSetList":[
31+
"post_id",
32+
"author"
33+
],
34+
"selectionSetGraphQL":"{\n post_id\n author\n}",
35+
"fieldName":"relatedPosts",
36+
"parentTypeName":"Post",
37+
"variables":{}
38+
}
39+
},
40+
{
41+
"arguments":{},
42+
"identity":"None",
43+
"source":{
44+
"post_id":"1",
45+
"author":"Author1"
46+
},
47+
"prev":"None",
48+
"info":{
49+
"selectionSetList":[
50+
"post_id",
51+
"author"
52+
],
53+
"selectionSetGraphQL":"{\n post_id\n author\n}",
54+
"fieldName":"relatedPosts",
55+
"parentTypeName":"Post",
56+
"variables":{}
57+
}
58+
}
59+
]

0 commit comments

Comments
 (0)