Skip to content

Commit 499adf1

Browse files
committed
docs(appsync): rewrite and split async resolvers
1 parent ec4fcd7 commit 499adf1

File tree

2 files changed

+39
-23
lines changed

2 files changed

+39
-23
lines changed

docs/core/event_handler/appsync.md

+3-23
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ You can nest `app.resolver()` decorator multiple times when resolving fields wit
108108

109109
=== "nested_mappings.py"
110110

111-
```python hl_lines="2 8 18-19 21 28"
111+
```python hl_lines="4 10 20-21 23 30"
112112
--8<-- "examples/event_handler_graphql/src/nested_mappings.py"
113113
```
114114

@@ -122,28 +122,8 @@ You can nest `app.resolver()` decorator multiple times when resolving fields wit
122122

123123
For Lambda Python3.8+ runtime, this utility supports async functions when you use in conjunction with `asyncio.run`.
124124

125-
```python hl_lines="5 9 11-13 21" title="Resolving GraphQL resolvers async"
126-
import asyncio
127-
from aws_lambda_powertools import Logger, Tracer
128-
129-
from aws_lambda_powertools.logging import correlation_paths
130-
from aws_lambda_powertools.event_handler import AppSyncResolver
131-
132-
tracer = Tracer(service="sample_resolver")
133-
logger = Logger(service="sample_resolver")
134-
app = AppSyncResolver()
135-
136-
@app.resolver(type_name="Query", field_name="listTodos")
137-
async def list_todos():
138-
todos = await some_async_io_call()
139-
return todos
140-
141-
@logger.inject_lambda_context(correlation_id_path=correlation_paths.APPSYNC_RESOLVER)
142-
@tracer.capture_lambda_handler
143-
def lambda_handler(event, context):
144-
result = app.resolve(event, context)
145-
146-
return asyncio.run(result)
125+
```python hl_lines="7 14 24-25 34 36" title="Resolving GraphQL resolvers async"
126+
--8<-- "examples/event_handler_graphql/src/async_resolvers.py"
147127
```
148128

149129
### Amplify GraphQL Transformer
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import asyncio
2+
from typing import TypedDict
3+
4+
import aiohttp
5+
6+
from aws_lambda_powertools import Logger, Tracer
7+
from aws_lambda_powertools.event_handler import AppSyncResolver
8+
from aws_lambda_powertools.logging import correlation_paths
9+
from aws_lambda_powertools.tracing import aiohttp_trace_config
10+
from aws_lambda_powertools.utilities.typing import LambdaContext
11+
12+
tracer = Tracer()
13+
logger = Logger()
14+
app = AppSyncResolver()
15+
16+
17+
class Todo(TypedDict):
18+
id: str # noqa AA03 VNE003, required due to GraphQL Schema
19+
userId: str
20+
title: str
21+
completed: bool
22+
23+
24+
@app.resolver(type_name="Query", field_name="listTodos")
25+
async def list_todos() -> list[Todo]:
26+
async with aiohttp.ClientSession(trace_configs=[aiohttp_trace_config()]) as session:
27+
async with session.get("https://jsonplaceholder.typicode.com/todos") as resp:
28+
return await resp.json()
29+
30+
31+
@logger.inject_lambda_context(correlation_id_path=correlation_paths.APPSYNC_RESOLVER)
32+
@tracer.capture_lambda_handler
33+
def lambda_handler(event: dict, context: LambdaContext) -> dict:
34+
result = app.resolve(event, context)
35+
36+
return asyncio.run(result)

0 commit comments

Comments
 (0)