Skip to content

Commit 4531757

Browse files
committed
docs(appsync_handler): add custom models and examples
1 parent 0612b21 commit 4531757

File tree

2 files changed

+160
-0
lines changed

2 files changed

+160
-0
lines changed

aws_lambda_powertools/event_handler/appsync.py

+48
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,54 @@ def resolve(
7878
model:
7979
Your data model to decode AppSync event, by default AppSyncResolverEvent
8080
81+
Example
82+
-------
83+
84+
```python
85+
from aws_lambda_powertools.event_handler import AppSyncResolver
86+
from aws_lambda_powertools.utilities.typing import LambdaContext
87+
88+
@app.resolver(field_name="createSomething")
89+
def create_something(id: str): # noqa AA03 VNE003
90+
return id
91+
92+
def handler(event, context: LambdaContext):
93+
return app.resolve(event, context)
94+
```
95+
96+
**Bringing custom models**
97+
98+
```python
99+
from aws_lambda_powertools import Logger, Tracer
100+
101+
from aws_lambda_powertools.logging import correlation_paths
102+
from aws_lambda_powertools.event_handler import AppSyncResolver
103+
104+
tracer = Tracer(service="sample_resolver")
105+
logger = Logger(service="sample_resolver")
106+
app = AppSyncResolver()
107+
108+
109+
class MyCustomModel(AppSyncResolverEvent):
110+
@property
111+
def country_viewer(self) -> str:
112+
return self.request_headers.get("cloudfront-viewer-country")
113+
114+
115+
@app.resolver(field_name="listLocations")
116+
@app.resolver(field_name="locations")
117+
def get_locations(name: str, description: str = ""):
118+
if app.current_event.country_viewer == "US":
119+
...
120+
return name + description
121+
122+
123+
@logger.inject_lambda_context(correlation_id_path=correlation_paths.APPSYNC_RESOLVER)
124+
@tracer.capture_lambda_handler
125+
def lambda_handler(event, context):
126+
return app.resolve(event, context, model=MyCustomModel)
127+
```
128+
81129
Returns
82130
-------
83131
Any

docs/core/event_handler/appsync.md

+112
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,118 @@ Use the following code for `merchantInfo` and `searchMerchant` functions respect
598598
}
599599
```
600600

601+
### Custom models
602+
603+
You can subclass `AppSyncResolverEvent` to bring your own set of methods to handle incoming events, by using `model` param in the `resolve` method.
604+
605+
606+
=== "custom_model.py"
607+
608+
```python hl_lines="11-14 19 26"
609+
from aws_lambda_powertools import Logger, Tracer
610+
611+
from aws_lambda_powertools.logging import correlation_paths
612+
from aws_lambda_powertools.event_handler import AppSyncResolver
613+
614+
tracer = Tracer(service="sample_resolver")
615+
logger = Logger(service="sample_resolver")
616+
app = AppSyncResolver()
617+
618+
619+
class MyCustomModel(AppSyncResolverEvent):
620+
@property
621+
def country_viewer(self) -> str:
622+
return self.request_headers.get("cloudfront-viewer-country")
623+
624+
@app.resolver(field_name="listLocations")
625+
@app.resolver(field_name="locations")
626+
def get_locations(name: str, description: str = ""):
627+
if app.current_event.country_viewer == "US":
628+
...
629+
return name + description
630+
631+
@logger.inject_lambda_context(correlation_id_path=correlation_paths.APPSYNC_RESOLVER)
632+
@tracer.capture_lambda_handler
633+
def lambda_handler(event, context):
634+
return app.resolve(event, context, model=MyCustomModel)
635+
```
636+
637+
=== "schema.graphql"
638+
639+
```typescript hl_lines="6 20"
640+
schema {
641+
query: Query
642+
}
643+
644+
type Query {
645+
listLocations: [Location]
646+
}
647+
648+
type Location {
649+
id: ID!
650+
name: String!
651+
description: String
652+
address: String
653+
}
654+
655+
type Merchant {
656+
id: String!
657+
name: String!
658+
description: String
659+
locations: [Location]
660+
}
661+
```
662+
663+
=== "listLocations_event.json"
664+
665+
```json
666+
{
667+
"arguments": {},
668+
"identity": null,
669+
"source": null,
670+
"request": {
671+
"headers": {
672+
"x-forwarded-for": "1.2.3.4, 5.6.7.8",
673+
"accept-encoding": "gzip, deflate, br",
674+
"cloudfront-viewer-country": "NL",
675+
"cloudfront-is-tablet-viewer": "false",
676+
"referer": "https://eu-west-1.console.aws.amazon.com/appsync/home?region=eu-west-1",
677+
"via": "2.0 9fce949f3749407c8e6a75087e168b47.cloudfront.net (CloudFront)",
678+
"cloudfront-forwarded-proto": "https",
679+
"origin": "https://eu-west-1.console.aws.amazon.com",
680+
"x-api-key": "da1-c33ullkbkze3jg5hf5ddgcs4fq",
681+
"content-type": "application/json",
682+
"x-amzn-trace-id": "Root=1-606eb2f2-1babc433453a332c43fb4494",
683+
"x-amz-cf-id": "SJw16ZOPuMZMINx5Xcxa9pB84oMPSGCzNOfrbJLvd80sPa0waCXzYQ==",
684+
"content-length": "114",
685+
"x-amz-user-agent": "AWS-Console-AppSync/",
686+
"x-forwarded-proto": "https",
687+
"host": "ldcvmkdnd5az3lm3gnf5ixvcyy.appsync-api.eu-west-1.amazonaws.com",
688+
"accept-language": "en-US,en;q=0.5",
689+
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Firefox/78.0",
690+
"cloudfront-is-desktop-viewer": "true",
691+
"cloudfront-is-mobile-viewer": "false",
692+
"accept": "*/*",
693+
"x-forwarded-port": "443",
694+
"cloudfront-is-smarttv-viewer": "false"
695+
}
696+
},
697+
"prev": null,
698+
"info": {
699+
"parentTypeName": "Query",
700+
"selectionSetList": [
701+
"id",
702+
"name",
703+
"description"
704+
],
705+
"selectionSetGraphQL": "{\n id\n name\n description\n}",
706+
"fieldName": "listLocations",
707+
"variables": {}
708+
},
709+
"stash": {}
710+
}
711+
```
712+
601713
## Testing your code
602714

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

0 commit comments

Comments
 (0)