Skip to content

Commit 2b82642

Browse files
committed
docs(event-handler): Initial markdown docs
1 parent f4c07aa commit 2b82642

File tree

5 files changed

+162
-0
lines changed

5 files changed

+162
-0
lines changed

aws_lambda_powertools/utilities/data_classes/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
"""
2+
Event Source Data Classes utility provides classes self-describing Lambda event sources.
3+
"""
4+
15
from .alb_event import ALBEvent
26
from .api_gateway_proxy_event import APIGatewayProxyEvent, APIGatewayProxyEventV2
37
from .appsync_resolver_event import AppSyncResolverEvent
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
"""
2+
Event handler decorators for common Lambda events
3+
"""
4+
15
from .appsync import AppSyncResolver
26

37
__all__ = ["AppSyncResolver"]

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ aws serverlessrepo list-application-versions \
152152
| [Event source data classes](./utilities/data_classes) | Data classes describing the schema of common Lambda event triggers
153153
| [Parser](./utilities/parser) | Data parsing and deep validation using Pydantic
154154
| [Idempotency](./utilities/idempotency) | Idempotent Lambda handler
155+
| [Event handler](./utilities/event_handler) | Event handler decorators for common Lambda events
155156

156157
## Environment variables
157158

docs/utilities/event_handler.md

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
---
2+
title: Event Handler
3+
description: Utility
4+
---
5+
6+
Event handler decorators for common Lambda events
7+
8+
9+
### AppSync Resolver Decorator
10+
11+
> New in 1.14.0
12+
13+
14+
=== "app.py"
15+
16+
```python hl_lines="1-3 6 8-9 13-14 18-19 23 25"
17+
from aws_lambda_powertools.logging import Logger, correlation_paths
18+
from aws_lambda_powertools.utilities.data_classes import AppSyncResolverEvent
19+
from aws_lambda_powertools.utilities.event_handler import AppSyncResolver
20+
21+
logger = Logger()
22+
app = AppSyncResolver()
23+
24+
@app.resolver(type_name="Query", field_name="listLocations", include_event=True)
25+
def list_locations(event: AppSyncResolverEvent, page: int = 0, size: int = 10):
26+
# Your logic to fetch locations
27+
...
28+
29+
@app.resolver(type_name="Merchant", field_name="extraInfo", include_event=True)
30+
def get_extra_info(event: AppSyncResolverEvent):
31+
# Can use "event.source" to filter within the parent context
32+
...
33+
34+
@app.resolver(field_name="commonField")
35+
def common_field():
36+
# Would match all fieldNames matching 'commonField'
37+
...
38+
39+
@logger.inject_lambda_context(correlation_id_path=correlation_paths.APPSYNC_RESOLVER)
40+
def handle(event, context):
41+
app.resolve(event, context)
42+
```
43+
=== "schema.graphql"
44+
45+
```graphql hl_lines="7-10 17-18 22-23"
46+
@model
47+
type Merchant
48+
{
49+
id: String!
50+
name: String!
51+
description: String
52+
# Resolves to `get_extra_info`
53+
extraInfo: ExtraInfo @function(name: "merchantInformation-${env}")
54+
# Resolves to `common_field`
55+
commonField: String @function(name: "merchantInformation-${env}")
56+
}
57+
58+
type Location {
59+
id: ID!
60+
name: String!
61+
address: Address
62+
# Resolves to `common_field`
63+
commonField: String @function(name: "merchantInformation-${env}")
64+
}
65+
66+
type Query {
67+
# List of locations resolves to `list_locations`
68+
listLocations(page: Int, size: Int): [Location] @function(name: "merchantInformation-${env}")
69+
}
70+
```
71+
=== "Example list_locations event"
72+
73+
```json hl_lines="2-7"
74+
{
75+
"typeName": "Query",
76+
"fieldName": "listLocations",
77+
"arguments": {
78+
"page": 2,
79+
"size": 1
80+
},
81+
"identity": {
82+
"claims": {
83+
"iat": 1615366261
84+
...
85+
},
86+
"username": "mike",
87+
...
88+
},
89+
"request": {
90+
"headers": {
91+
"x-amzn-trace-id": "Root=1-60488877-0b0c4e6727ab2a1c545babd0",
92+
"x-forwarded-for": "127.0.0.1"
93+
...
94+
}
95+
},
96+
...
97+
}
98+
```
99+
100+
=== "Example get_extra_info event"
101+
102+
```json hl_lines="2 3"
103+
{
104+
"typeName": "Merchant",
105+
"fieldName": "extraInfo",
106+
"arguments": {
107+
},
108+
"identity": {
109+
"claims": {
110+
"iat": 1615366261
111+
...
112+
},
113+
"username": "mike",
114+
...
115+
},
116+
"request": {
117+
"headers": {
118+
"x-amzn-trace-id": "Root=1-60488877-0b0c4e6727ab2a1c545babd0",
119+
"x-forwarded-for": "127.0.0.1"
120+
...
121+
}
122+
},
123+
...
124+
}
125+
```
126+
127+
=== "Example common_field event"
128+
129+
```json hl_lines="2 3"
130+
{
131+
"typeName": "Merchant",
132+
"fieldName": "commonField",
133+
"arguments": {
134+
},
135+
"identity": {
136+
"claims": {
137+
"iat": 1615366261
138+
...
139+
},
140+
"username": "mike",
141+
...
142+
},
143+
"request": {
144+
"headers": {
145+
"x-amzn-trace-id": "Root=1-60488877-0b0c4e6727ab2a1c545babd0",
146+
"x-forwarded-for": "127.0.0.1"
147+
...
148+
}
149+
},
150+
...
151+
}
152+
```

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ nav:
1818
- utilities/data_classes.md
1919
- utilities/parser.md
2020
- utilities/idempotency.md
21+
- utilities/event_handler.md
2122

2223
theme:
2324
name: material

0 commit comments

Comments
 (0)