Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(event-handler): improve testing section for graphql #996

Merged
merged 4 commits into from
Feb 4, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 67 additions & 12 deletions docs/core/event_handler/appsync.md
Original file line number Diff line number Diff line change
Expand Up @@ -775,27 +775,82 @@ You can test your resolvers by passing a mocked or actual AppSync Lambda event t

You can use either `app.resolve(event, context)` or simply `app(event, context)`.

Here's an example from our internal functional test.
Here's an example of how you can test your synchronous resolvers:

=== "test_direct_resolver.py"
=== "test_resolver.py"

```python
import pytest
from src.index import app # import the instance of AppSyncResolver from your code

def test_direct_resolver():
# Check whether we can handle an example appsync direct resolver
# load_event primarily deserialize the JSON event into a dict
mock_event = load_event("appSyncDirectResolver.json")
# Load mock event from a file
json_file = "appSyncDirectResolver.json"
fh = open(file)
mock_event = json.load(fh)
to-mc marked this conversation as resolved.
Show resolved Hide resolved

# Call the implicit handler
result = app(mock_event, {})

assert result == "created this value"
```

=== "src/index.py"

```python

from aws_lambda_powertools.event_handler import AppSyncResolver

app = AppSyncResolver()

@app.resolver(field_name="createSomething")
def create_something():
return "created this value"

```

=== "appSyncDirectResolver.json"

```json
--8<-- "tests/events/appSyncDirectResolver.json"
```

And an example for testing asynchronous resolvers. Note that this requires the `pytest-asyncio` package:

app = AppSyncResolver()

@app.resolver(field_name="createSomething")
def create_something(id: str):
assert app.lambda_context == {}
return id
=== "test_async_resolver.py"

```python
import pytest
from src.index import app # import the instance of AppSyncResolver from your code

@pytest.mark.asyncio
async def test_direct_resolver():
# Load mock event from a file
json_file = "appSyncDirectResolver.json"
fh = open(file)
mock_event = json.load(fh)
to-mc marked this conversation as resolved.
Show resolved Hide resolved

# Call the implicit handler
result = app(mock_event, {})
result = await app(mock_event, {})

assert result == "created this value"
```

=== "src/index.py"

```python
import asyncio

from aws_lambda_powertools.event_handler import AppSyncResolver

app = AppSyncResolver()

@app.resolver(field_name="createSomething")
async def create_something_async():
await asyncio.sleep(1) # Do async stuff
return "created this value"

assert result == "my identifier"
```

=== "appSyncDirectResolver.json"
Expand Down