@@ -775,27 +775,88 @@ You can test your resolvers by passing a mocked or actual AppSync Lambda event t
775
775
776
776
You can use either ` app.resolve(event, context) ` or simply ` app(event, context) ` .
777
777
778
- Here's an example from our internal functional test.
778
+ Here's an example of how you can test your synchronous resolvers:
779
779
780
- === "test_direct_resolver .py"
780
+ === "test_resolver .py"
781
781
782
782
```python
783
- def test_direct_resolver():
784
- # Check whether we can handle an example appsync direct resolver
785
- # load_event primarily deserialize the JSON event into a dict
786
- mock_event = load_event("appSyncDirectResolver.json")
783
+ import json
784
+ import pytest
785
+ from pathlib import Path
787
786
788
- app = AppSyncResolver()
787
+ from src.index import app # import the instance of AppSyncResolver from your code
789
788
790
- @app.resolver(field_name="createSomething")
791
- def create_something(id: str):
792
- assert app.lambda_context == {}
793
- return id
789
+ def test_direct_resolver():
790
+ # Load mock event from a file
791
+ json_file_path = Path("appSyncDirectResolver.json")
792
+ with open(json_file_path) as json_file:
793
+ mock_event = json.load(json_file)
794
794
795
795
# Call the implicit handler
796
796
result = app(mock_event, {})
797
797
798
- assert result == "my identifier"
798
+ assert result == "created this value"
799
+ ```
800
+
801
+ === "src/index.py"
802
+
803
+ ```python
804
+
805
+ from aws_lambda_powertools.event_handler import AppSyncResolver
806
+
807
+ app = AppSyncResolver()
808
+
809
+ @app.resolver(field_name="createSomething")
810
+ def create_something():
811
+ return "created this value"
812
+
813
+ ```
814
+
815
+ === "appSyncDirectResolver.json"
816
+
817
+ ```json
818
+ --8<-- "tests/events/appSyncDirectResolver.json"
819
+ ```
820
+
821
+ And an example for testing asynchronous resolvers. Note that this requires the ` pytest-asyncio ` package:
822
+
823
+
824
+ === "test_async_resolver.py"
825
+
826
+ ```python
827
+ import json
828
+ import pytest
829
+ from pathlib import Path
830
+
831
+ from src.index import app # import the instance of AppSyncResolver from your code
832
+
833
+ @pytest.mark.asyncio
834
+ async def test_direct_resolver():
835
+ # Load mock event from a file
836
+ json_file_path = Path("appSyncDirectResolver.json")
837
+ with open(json_file_path) as json_file:
838
+ mock_event = json.load(json_file)
839
+
840
+ # Call the implicit handler
841
+ result = await app(mock_event, {})
842
+
843
+ assert result == "created this value"
844
+ ```
845
+
846
+ === "src/index.py"
847
+
848
+ ```python
849
+ import asyncio
850
+
851
+ from aws_lambda_powertools.event_handler import AppSyncResolver
852
+
853
+ app = AppSyncResolver()
854
+
855
+ @app.resolver(field_name="createSomething")
856
+ async def create_something_async():
857
+ await asyncio.sleep(1) # Do async stuff
858
+ return "created this value"
859
+
799
860
```
800
861
801
862
=== "appSyncDirectResolver.json"
0 commit comments