Skip to content

Commit

Permalink
Merge branch 'main' into release-v1.70.0
Browse files Browse the repository at this point in the history
  • Loading branch information
GavinZZ committed Jun 22, 2023
2 parents b5dbc6f + 063ac27 commit fdaa826
Show file tree
Hide file tree
Showing 102 changed files with 10,915 additions and 14 deletions.
101 changes: 101 additions & 0 deletions integration/combination/test_graphqlapi_lambda_resolver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import json
from unittest.case import skipIf

import requests

from integration.config.service_names import APP_SYNC
from integration.helpers.base_test import BaseTest
from integration.helpers.resource import current_region_does_not_support


def execute_and_verify_appsync_query(url, api_key, query):
"""
Executes a query to an AppSync GraphQLApi.
Also checks that the response is 200 and does not contain errors before returning.
"""
headers = {
"Content-Type": "application/json",
"x-api-key": api_key,
}
payload = {"query": query}

response = requests.post(url, json=payload, headers=headers)
response.raise_for_status()
data = response.json()
if "errors" in data:
raise Exception(json.dumps(data["errors"]))

return data


@skipIf(current_region_does_not_support([APP_SYNC]), "AppSync is not supported in this testing region")
class TestGraphQLApiPipelineResolver(BaseTest):
def test_api(self):
file_name = "combination/graphqlapi_lambda_resolver"
self.create_and_verify_stack(file_name)

outputs = self.get_stack_outputs()

author = "AUTHORNAME"
title = "Our first post!"
content = "This is our first post."

query = f"""
mutation addPost {{
addPost(
id: 100
author: "{author}"
title: "{title}"
content: "{content}"
) {{
id
author
title
content
}}
}}
"""

url = outputs["SuperCoolAPI"]
api_key = outputs["SuperCoolAPIMyApiKey"]

response = execute_and_verify_appsync_query(url, api_key, query)

add_post = response["data"]["addPost"]

self.assertEqual(add_post["id"], "100")
self.assertEqual(add_post["author"], author)
self.assertEqual(add_post["title"], title)
self.assertEqual(add_post["content"], content)

query = """
query getPost {
getPost(id:"1") {
id
author
title
content
ups
downs
}
}
"""

response = execute_and_verify_appsync_query(url, api_key, query)

get_post = response["data"]["getPost"]

# These values are hardcoded inside the Lambda function for a post with id "1".
author = "Author1"
title = "First book"
content = "Book 1 has this content"
ups = 100
downs = 10

self.assertEqual(get_post["id"], "1")
self.assertEqual(get_post["author"], author)
self.assertEqual(get_post["title"], title)
self.assertEqual(get_post["content"], content)
self.assertEqual(get_post["ups"], ups)
self.assertEqual(get_post["downs"], downs)
101 changes: 101 additions & 0 deletions integration/combination/test_graphqlapi_pipeline_resolver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import json
from unittest.case import skipIf

import requests

from integration.config.service_names import APP_SYNC
from integration.helpers.base_test import BaseTest
from integration.helpers.resource import current_region_does_not_support


def execute_and_verify_appsync_query(url, api_key, query):
"""
Executes a query to an AppSync GraphQLApi.
Also checks that the response is 200 and does not contain errors before returning.
"""
headers = {
"Content-Type": "application/json",
"x-api-key": api_key,
}
payload = {"query": query}

response = requests.post(url, json=payload, headers=headers)
response.raise_for_status()
data = response.json()
if "errors" in data:
raise Exception(json.dumps(data["errors"]))

return data


@skipIf(current_region_does_not_support([APP_SYNC]), "AppSync is not supported in this testing region")
class TestGraphQLApiPipelineResolver(BaseTest):
def test_api(self):
file_name = "combination/graphqlapi_pipeline_resolver"
self.create_and_verify_stack(file_name)

outputs = self.get_stack_outputs()

author = "AUTHORNAME"
title = "Our first post!"
content = "This is our first post."

query = f"""
mutation addPost {{
addPost(
author: "{author}"
title: "{title}"
content: "{content}"
) {{
id
author
title
content
ups
downs
version
}}
}}
"""

url = outputs["SuperCoolAPI"]
api_key = outputs["MyApiKey"]

response = execute_and_verify_appsync_query(url, api_key, query)

add_post = response["data"]["addPost"]

self.assertEqual(add_post["author"], author)
self.assertEqual(add_post["title"], title)
self.assertEqual(add_post["content"], content)
self.assertEqual(add_post["ups"], 1)
self.assertEqual(add_post["downs"], 0)
self.assertEqual(add_post["version"], 1)

post_id = add_post["id"]
query = f"""
query getPost {{
getPost(id:"{post_id}") {{
id
author
title
content
ups
downs
version
}}
}}
"""

response = execute_and_verify_appsync_query(url, api_key, query)

get_post = response["data"]["getPost"]

self.assertEqual(get_post["author"], author)
self.assertEqual(get_post["title"], title)
self.assertEqual(get_post["content"], content)
self.assertEqual(get_post["ups"], 1)
self.assertEqual(get_post["downs"], 0)
self.assertEqual(get_post["version"], 1)
self.assertEqual(get_post["id"], post_id)
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
[
{
"LogicalResourceId": "MyLambdaFunction",
"ResourceType": "AWS::Lambda::Function"
},
{
"LogicalResourceId": "LambdaFunctionRole",
"ResourceType": "AWS::IAM::Role"
},
{
"LogicalResourceId": "SuperCoolAPI",
"ResourceType": "AWS::AppSync::GraphQLApi"
},
{
"LogicalResourceId": "SuperCoolAPISchema",
"ResourceType": "AWS::AppSync::GraphQLSchema"
},
{
"LogicalResourceId": "SuperCoolAPICloudWatchRole",
"ResourceType": "AWS::IAM::Role"
},
{
"LogicalResourceId": "SuperCoolAPIMyApiKey",
"ResourceType": "AWS::AppSync::ApiKey"
},
{
"LogicalResourceId": "SuperCoolAPIMyLambdaDataSourceLambdaDataSource",
"ResourceType": "AWS::AppSync::DataSource"
},
{
"LogicalResourceId": "SuperCoolAPIMyLambdaDataSourceLambdaDataSourceRole",
"ResourceType": "AWS::IAM::Role"
},
{
"LogicalResourceId": "SuperCoolAPIMyLambdaDataSourceLambdaDataSourceToLambdaConnectorPolicy",
"ResourceType": "AWS::IAM::ManagedPolicy"
},
{
"LogicalResourceId": "SuperCoolAPIlambdaInvoker",
"ResourceType": "AWS::AppSync::FunctionConfiguration"
},
{
"LogicalResourceId": "SuperCoolAPIMutationaddPost",
"ResourceType": "AWS::AppSync::Resolver"
},
{
"LogicalResourceId": "SuperCoolAPIQuerygetPost",
"ResourceType": "AWS::AppSync::Resolver"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
[
{
"LogicalResourceId": "MyApiKey",
"ResourceType": "AWS::AppSync::ApiKey"
},
{
"LogicalResourceId": "DynamoDBPostsTable",
"ResourceType": "AWS::DynamoDB::Table"
},
{
"LogicalResourceId": "DynamoDBPostsLogTable",
"ResourceType": "AWS::DynamoDB::Table"
},
{
"LogicalResourceId": "SuperCoolAPI",
"ResourceType": "AWS::AppSync::GraphQLApi"
},
{
"LogicalResourceId": "SuperCoolAPISchema",
"ResourceType": "AWS::AppSync::GraphQLSchema"
},
{
"LogicalResourceId": "SuperCoolAPICloudWatchRole",
"ResourceType": "AWS::IAM::Role"
},
{
"LogicalResourceId": "SuperCoolAPIPostsDataSourceDynamoDBDataSource",
"ResourceType": "AWS::AppSync::DataSource"
},
{
"LogicalResourceId": "SuperCoolAPIPostsDataSourceDynamoDBDataSourceRole",
"ResourceType": "AWS::IAM::Role"
},
{
"LogicalResourceId": "SuperCoolAPIPostsDataSourceDynamoDBDataSourceToTableConnectorPolicy",
"ResourceType": "AWS::IAM::ManagedPolicy"
},
{
"LogicalResourceId": "SuperCoolAPIPostsLogDataSourceDynamoDBDataSource",
"ResourceType": "AWS::AppSync::DataSource"
},
{
"LogicalResourceId": "SuperCoolAPIPostsLogDataSourceDynamoDBDataSourceRole",
"ResourceType": "AWS::IAM::Role"
},
{
"LogicalResourceId": "SuperCoolAPIPostsLogDataSourceDynamoDBDataSourceToTableConnectorPolicy",
"ResourceType": "AWS::IAM::ManagedPolicy"
},
{
"LogicalResourceId": "SuperCoolAPIcreatePostItem",
"ResourceType": "AWS::AppSync::FunctionConfiguration"
},
{
"LogicalResourceId": "SuperCoolAPIcreatePostLogItem",
"ResourceType": "AWS::AppSync::FunctionConfiguration"
},
{
"LogicalResourceId": "SuperCoolAPIformatPostLogItem",
"ResourceType": "AWS::AppSync::FunctionConfiguration"
},
{
"LogicalResourceId": "SuperCoolAPIformatPostItem",
"ResourceType": "AWS::AppSync::FunctionConfiguration"
},
{
"LogicalResourceId": "SuperCoolAPIMutationaddPost",
"ResourceType": "AWS::AppSync::Resolver"
},
{
"LogicalResourceId": "SuperCoolAPIgetPostFromTable",
"ResourceType": "AWS::AppSync::FunctionConfiguration"
},
{
"LogicalResourceId": "SuperCoolAPIQuerygetPost",
"ResourceType": "AWS::AppSync::Resolver"
},
{
"LogicalResourceId": "SuperCoolAPINoneDataSource",
"ResourceType": "AWS::AppSync::DataSource"
}
]
Loading

0 comments on commit fdaa826

Please sign in to comment.