forked from aws-powertools/powertools-lambda-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappSyncResolver.py
57 lines (48 loc) · 2.1 KB
/
appSyncResolver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from aws_lambda_powertools.utilities.data_classes import event_source
from aws_lambda_powertools.utilities.data_classes.appsync_resolver_event import (
AppSyncIdentityCognito,
AppSyncResolverEvent,
)
from aws_lambda_powertools.utilities.typing import LambdaContext
@event_source(data_class=AppSyncResolverEvent)
def lambda_handler(event: AppSyncResolverEvent, context: LambdaContext):
# Access the AppSync event details
type_name = event.type_name
field_name = event.field_name
arguments = event.arguments
source = event.source
print(f"Resolving field '{field_name}' for type '{type_name}'")
print(f"Arguments: {arguments}")
print(f"Source: {source}")
# Check if the identity is Cognito-based
if isinstance(event.identity, AppSyncIdentityCognito):
user_id = event.identity.sub
username = event.identity.username
print(f"Request from Cognito user: {username} (ID: {user_id})")
else:
print("Request is not from a Cognito-authenticated user")
if type_name == "Merchant" and field_name == "locations":
page = arguments.get("page", 1)
size = arguments.get("size", 10)
name_filter = arguments.get("name")
# Here you would typically fetch locations from a database
# This is a mock implementation
locations = [
{"id": "1", "name": "Location 1", "address": "123 Main St"},
{"id": "2", "name": "Location 2", "address": "456 Elm St"},
{"id": "3", "name": "Location 3", "address": "789 Oak St"},
]
# Apply name filter if provided
if name_filter:
locations = [loc for loc in locations if name_filter.lower() in loc["name"].lower()]
# Apply pagination
start = (page - 1) * size
end = start + size
paginated_locations = locations[start:end]
return {
"items": paginated_locations,
"totalCount": len(locations),
"nextToken": str(page + 1) if end < len(locations) else None,
}
else:
raise Exception(f"Unhandled field: {field_name} for type: {type_name}")