-
Notifications
You must be signed in to change notification settings - Fork 434
feat(parser): add AppSyncResolver model #6400
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
leandrodamascena
merged 8 commits into
aws-powertools:develop
from
VatsalGoel3:feature/appsync-resolver-schema
Apr 4, 2025
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
80d4b87
feat(parser): add AppSync resolver event model to parser utility
VatsalGoel3 2e7e530
Updated documentation
VatsalGoel3 5daab4e
Merge branch 'develop' into feature/appsync-resolver-schema
VatsalGoel3 9b9ceca
Updated test code
VatsalGoel3 b5313e3
Merge branch 'develop' into feature/appsync-resolver-schema
VatsalGoel3 dcca673
Updated test code
VatsalGoel3 dc80b3f
Update test code
VatsalGoel3 3c4a83a
Merge branch 'develop' into feature/appsync-resolver-schema
leandrodamascena 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
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,70 @@ | ||
from typing import Optional, List, Dict, Union, Any | ||
from pydantic import BaseModel | ||
|
||
class AppSyncIamIdentity(BaseModel): | ||
accountId: str | ||
cognitoIdentityPoolId: Optional[str] | ||
cognitoIdentityId: Optional[str] | ||
sourceIp: List[str] | ||
username: str | ||
userArn: str | ||
cognitoIdentityAuthType: Optional[str] | ||
cognitoIdentityAuthProvider: Optional[str] | ||
|
||
|
||
class AppSyncCognitoIdentity(BaseModel): | ||
sub: str | ||
issuer: str | ||
username: str | ||
claims: Dict[str, Any] | ||
sourceIp: List[str] | ||
defaultAuthStrategy: str | ||
groups: Optional[List[str]] | ||
|
||
|
||
class AppSyncOidcIdentity(BaseModel): | ||
claims: Dict[str, Any] | ||
issuer: str | ||
sub: str | ||
|
||
|
||
class AppSyncLambdaIdentity(BaseModel): | ||
resolverContext: Dict[str, Any] | ||
|
||
|
||
AppSyncIdentity = Union[ | ||
AppSyncIamIdentity, | ||
AppSyncCognitoIdentity, | ||
AppSyncOidcIdentity, | ||
AppSyncLambdaIdentity, | ||
] | ||
|
||
|
||
class AppSyncRequestModel(BaseModel): | ||
domainName: Optional[str] | ||
headers: Dict[str, str] | ||
|
||
|
||
class AppSyncInfoModel(BaseModel): | ||
selectionSetList: List[str] | ||
selectionSetGraphQL: str | ||
parentTypeName: str | ||
fieldName: str | ||
variables: Dict[str, Any] | ||
|
||
|
||
class AppSyncPrevModel(BaseModel): | ||
result: Dict[str, Any] | ||
|
||
|
||
class AppSyncResolverEventModel(BaseModel): | ||
arguments: Dict[str, Any] | ||
identity: Optional[AppSyncIdentity] | ||
source: Optional[Dict[str, Any]] | ||
request: AppSyncRequestModel | ||
info: AppSyncInfoModel | ||
prev: Optional[AppSyncPrevModel] | ||
stash: Dict[str, Any] | ||
|
||
|
||
AppSyncBatchResolverEventModel = List[AppSyncResolverEventModel] |
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
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,78 @@ | ||
{ | ||
"typeName": "Merchant", | ||
"fieldName": "locations", | ||
"arguments": { | ||
"page": 2, | ||
"size": 1, | ||
"name": "value" | ||
}, | ||
"identity": { | ||
"claims": { | ||
"sub": "07920713-4526-4642-9c88-2953512de441", | ||
"iss": "https://cognito-idp.us-east-1.amazonaws.com/us-east-1_POOL_ID", | ||
"aud": "58rc9bf5kkti90ctmvioppukm9", | ||
"event_id": "7f4c9383-abf6-48b7-b821-91643968b755", | ||
"token_use": "id", | ||
"auth_time": 1615366261, | ||
"name": "Michael Brewer", | ||
"exp": 1615369861, | ||
"iat": 1615366261 | ||
}, | ||
"defaultAuthStrategy": "ALLOW", | ||
"groups": null, | ||
"issuer": "https://cognito-idp.us-east-1.amazonaws.com/us-east-1_POOL_ID", | ||
"sourceIp": ["11.215.2.22"], | ||
"sub": "07920713-4526-4642-9c88-2953512de441", | ||
"username": "mike" | ||
}, | ||
"source": { | ||
"name": "Value", | ||
"nested": { | ||
"name": "value", | ||
"list": [] | ||
} | ||
}, | ||
"request": { | ||
"headers": { | ||
"x-forwarded-for": "11.215.2.22, 64.44.173.11", | ||
"cloudfront-viewer-country": "US", | ||
"cloudfront-is-tablet-viewer": "false", | ||
"via": "2.0 SOMETHING.cloudfront.net (CloudFront)", | ||
"cloudfront-forwarded-proto": "https", | ||
"origin": "https://console.aws.amazon.com", | ||
"content-length": "156", | ||
"accept-language": "en-US,en;q=0.9", | ||
"host": "SOMETHING.appsync-api.us-east-1.amazonaws.com", | ||
"x-forwarded-proto": "https", | ||
"sec-gpc": "1", | ||
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)", | ||
"accept": "*/*", | ||
"cloudfront-is-mobile-viewer": "false", | ||
"cloudfront-is-smarttv-viewer": "false", | ||
"accept-encoding": "gzip, deflate, br", | ||
"referer": "https://console.aws.amazon.com/", | ||
"content-type": "application/json", | ||
"sec-fetch-mode": "cors", | ||
"x-amz-cf-id": "Fo5VIuvP6V6anIEt62WzFDCK45mzM4yEdpt5BYxOl9OFqafd-WR0cA==", | ||
"x-amzn-trace-id": "Root=1-60488877-0b0c4e6727ab2a1c545babd0", | ||
"authorization": "AUTH-HEADER", | ||
"sec-fetch-dest": "empty", | ||
"x-amz-user-agent": "AWS-Console-AppSync/", | ||
"cloudfront-is-desktop-viewer": "true", | ||
"sec-fetch-site": "cross-site", | ||
"x-forwarded-port": "443" | ||
}, | ||
"domainName": "SOMETHING.appsync-api.us-east-1.amazonaws.com" | ||
}, | ||
"prev": { | ||
"result": {} | ||
}, | ||
"info": { | ||
"selectionSetList": ["id", "field1", "field2"], | ||
"selectionSetGraphQL": "{\n id\n field1\n field2\n}", | ||
"parentTypeName": "Merchant", | ||
"fieldName": "locations", | ||
"variables": {} | ||
}, | ||
"stash": {} | ||
} |
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,43 @@ | ||
import json | ||
import pathlib | ||
import pytest | ||
|
||
from aws_lambda_powertools.utilities.parser import parse, ValidationError | ||
from aws_lambda_powertools.utilities.parser.models.appsync import AppSyncResolverEventModel | ||
|
||
|
||
def load_event(filename: str) -> dict: | ||
""" | ||
Load a JSON event from the events directory. | ||
|
||
The function navigates four levels up from the current file to locate the | ||
`tests/events` folder. | ||
""" | ||
event_path = pathlib.Path(__file__).parent.parent.parent.parent / "events" / filename | ||
with event_path.open() as f: | ||
return json.load(f) | ||
leandrodamascena marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def test_appsync_event_model_parses_successfully(): | ||
""" | ||
Validate that a valid AppSync resolver event is correctly parsed by the model. | ||
""" | ||
event = load_event("appsync_resolver_event.json") | ||
|
||
parsed_event = parse(event=event, model=AppSyncResolverEventModel) | ||
|
||
assert parsed_event.arguments["page"] == 2 | ||
assert parsed_event.identity.username == "mike" | ||
assert parsed_event.request.headers["host"].endswith("appsync-api.us-east-1.amazonaws.com") | ||
assert parsed_event.info.fieldName == "locations" | ||
assert parsed_event.info.parentTypeName == "Merchant" | ||
|
||
|
||
def test_appsync_event_model_invalid_payload_raises(): | ||
""" | ||
Validate that parsing an invalid AppSync resolver event payload raises a ValidationError. | ||
""" | ||
invalid_event = {"invalid": "event"} | ||
|
||
with pytest.raises(ValidationError): | ||
parse(event=invalid_event, model=AppSyncResolverEventModel) |
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.