@@ -598,6 +598,118 @@ Use the following code for `merchantInfo` and `searchMerchant` functions respect
598
598
}
599
599
```
600
600
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
+
601
713
## Testing your code
602
714
603
715
You can test your resolvers by passing a mocked or actual AppSync Lambda event that you're expecting.
0 commit comments