Skip to content

Commit c3dd07f

Browse files
committed
* 'develop' of https://github.com/awslabs/aws-lambda-powertools-python: docs(appsync): add new router feature (aws-powertools#821) docs(tenets): update Idiomatic tenet to Progressive (aws-powertools#823) docs: use higher contrast font (aws-powertools#822)
2 parents 22469c2 + c2aaddc commit c3dd07f

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

docs/core/event_handler/appsync.md

+58
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,64 @@ You can subclass `AppSyncResolverEvent` to bring your own set of methods to hand
711711
}
712712
```
713713

714+
### Split operations with Router
715+
716+
As you grow the number of related GraphQL operations a given Lambda function should handle, it is natural to split them into separate files to ease maintenance - That's where the `Router` feature is useful.
717+
718+
Let's assume you have `app.py` as your Lambda function entrypoint and routes in `users.py`, this is how you'd use the `Router` feature.
719+
720+
=== "resolvers/location.py"
721+
722+
We import **Router** instead of **AppSyncResolver**; syntax wise is exactly the same.
723+
724+
```python hl_lines="4 7 10 15"
725+
from typing import Any, Dict, List
726+
727+
from aws_lambda_powertools import Logger
728+
from aws_lambda_powertools.event_handler.appsync import Router
729+
730+
logger = Logger(child=True)
731+
router = Router()
732+
733+
734+
@router.resolver(type_name="Query", field_name="listLocations")
735+
def list_locations(merchant_id: str) -> List[Dict[str, Any]]:
736+
return [{"name": "Location name", "merchant_id": merchant_id}]
737+
738+
739+
@router.resolver(type_name="Location", field_name="status")
740+
def resolve_status(merchant_id: str) -> str:
741+
logger.debug(f"Resolve status for merchant_id: {merchant_id}")
742+
return "FOO"
743+
```
744+
745+
=== "app.py"
746+
747+
We use `include_router` method and include all location operations registered in the `router` global object.
748+
749+
```python hl_lines="8 13"
750+
from typing import Dict
751+
752+
from aws_lambda_powertools import Logger, Tracer
753+
from aws_lambda_powertools.event_handler import AppSyncResolver
754+
from aws_lambda_powertools.logging.correlation_paths import APPSYNC_RESOLVER
755+
from aws_lambda_powertools.utilities.typing import LambdaContext
756+
757+
from resolvers import location
758+
759+
tracer = Tracer()
760+
logger = Logger()
761+
app = AppSyncResolver()
762+
app.include_router(location.router)
763+
764+
765+
@tracer.capture_lambda_handler
766+
@logger.inject_lambda_context(correlation_id_path=APPSYNC_RESOLVER)
767+
def lambda_handler(event: Dict, context: LambdaContext):
768+
app.resolve(event, context)
769+
```
770+
771+
714772
## Testing your code
715773

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

0 commit comments

Comments
 (0)