-
Notifications
You must be signed in to change notification settings - Fork 429
feat(event-handler): Add AppSync handler decorator #363
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
Merged
heitorlessa
merged 19 commits into
aws-powertools:develop
from
gyft:feat-event-handler-appsync
Mar 31, 2021
Merged
Changes from 18 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
ce80a90
feat(event-handler): Add AppSync handler decorator
michaelbrewer c7709bf
test(event-handler): Use pathlib
michaelbrewer f145c04
fix(tracer): Correct type hint for MyPy
michaelbrewer f4c07aa
Merge branch 'develop' into feat-event-handler-appsync
michaelbrewer 2b82642
docs(event-handler): Initial markdown docs
michaelbrewer 9f4e3e8
feat(event-handler): Add an implicit appsync handler
michaelbrewer 3f664d7
docs(event-handler): Add implicit handler example
michaelbrewer 4f43365
docs(event-handler): Add full amplify example
michaelbrewer 36b088e
chore(event-handler): Add more docs
michaelbrewer 347091f
chore(event-handler): Add current_event
michaelbrewer d969e98
Merge branch 'develop' into feat-event-handler-appsync
michaelbrewer 7d98715
refactor(event-handler): Remove include_event and incluce_context opt…
michaelbrewer 240b0a6
chore: bump ci
michaelbrewer f009f56
Merge branch 'develop' into feat-event-handler-appsync
michaelbrewer dc93055
Merge branch 'develop' into feat-event-handler-appsync
michaelbrewer 8f04851
refactor(event-handler): Move to up to
michaelbrewer daa56f9
chore(event-handler): Add debug logging to appsync resolver
michaelbrewer 02818db
docs(event-handler): Add more detailed example to docstring
michaelbrewer e778cf0
chore(event-handler): Rename to _get_resolver
michaelbrewer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
""" | ||
Event handler decorators for common Lambda events | ||
""" | ||
|
||
from .appsync import AppSyncResolver | ||
|
||
__all__ = ["AppSyncResolver"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import logging | ||
from typing import Any, Callable | ||
|
||
from aws_lambda_powertools.utilities.data_classes import AppSyncResolverEvent | ||
from aws_lambda_powertools.utilities.typing import LambdaContext | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class AppSyncResolver: | ||
""" | ||
AppSync resolver decorator | ||
|
||
Example | ||
------- | ||
|
||
**Sample usage** | ||
|
||
from aws_lambda_powertools.event_handler import AppSyncResolver | ||
|
||
app = AppSyncResolver() | ||
|
||
@app.resolver(type_name="Query", field_name="listLocations") | ||
def list_locations(page: int = 0, size: int = 10) -> list: | ||
# Your logic to fetch locations with arguments passed in | ||
return [{"id": 100, "name": "Smooth Grooves"}] | ||
|
||
@app.resolver(type_name="Merchant", field_name="extraInfo") | ||
def get_extra_info() -> dict: | ||
# Can use "app.current_event.source" to filter within the parent context | ||
account_type = app.current_event.source["accountType"] | ||
method = "BTC" if account_type == "NEW" else "USD" | ||
return {"preferredPaymentMethod": method} | ||
|
||
@app.resolver(field_name="commonField") | ||
def common_field() -> str: | ||
# Would match all fieldNames matching 'commonField' | ||
return str(uuid.uuid4()) | ||
""" | ||
|
||
current_event: AppSyncResolverEvent | ||
lambda_context: LambdaContext | ||
|
||
def __init__(self): | ||
self._resolvers: dict = {} | ||
|
||
def resolver(self, type_name: str = "*", field_name: str = None): | ||
"""Registers the resolver for field_name | ||
|
||
Parameters | ||
---------- | ||
type_name : str | ||
Type name | ||
field_name : str | ||
Field name | ||
""" | ||
|
||
def register_resolver(func): | ||
logger.debug(f"Adding resolver `{func.__name__}` for field `{type_name}.{field_name}`") | ||
self._resolvers[f"{type_name}.{field_name}"] = {"func": func} | ||
return func | ||
|
||
return register_resolver | ||
|
||
def resolve(self, event: dict, context: LambdaContext) -> Any: | ||
"""Resolve field_name | ||
|
||
Parameters | ||
---------- | ||
event : dict | ||
Lambda event | ||
context : LambdaContext | ||
Lambda context | ||
|
||
Returns | ||
------- | ||
Any | ||
Returns the result of the resolver | ||
|
||
Raises | ||
------- | ||
ValueError | ||
If we could not find a field resolver | ||
""" | ||
self.current_event = AppSyncResolverEvent(event) | ||
self.lambda_context = context | ||
resolver = self._resolver(self.current_event.type_name, self.current_event.field_name) | ||
return resolver(**self.current_event.arguments) | ||
|
||
def _resolver(self, type_name: str, field_name: str) -> Callable: | ||
heitorlessa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Find resolver for field_name | ||
|
||
Parameters | ||
---------- | ||
type_name : str | ||
Type name | ||
field_name : str | ||
Field name | ||
|
||
Returns | ||
------- | ||
Callable | ||
callable function and configuration | ||
""" | ||
full_name = f"{type_name}.{field_name}" | ||
resolver = self._resolvers.get(full_name, self._resolvers.get(f"*.{field_name}")) | ||
if not resolver: | ||
raise ValueError(f"No resolver found for '{full_name}'") | ||
return resolver["func"] | ||
|
||
def __call__(self, event, context) -> Any: | ||
"""Implicit lambda handler which internally calls `resolve`""" | ||
return self.resolve(event, context) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 0 additions & 50 deletions
50
aws_lambda_powertools/utilities/data_classes/appsync/resolver_utils.py
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.