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

feat: Add python example for appsync-graphql-dynamodb #121

Merged
merged 7 commits into from
Sep 17, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 11 additions & 0 deletions python/appsync-graphql-dynamodb/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env python3

from aws_cdk import core

from app_sync_cdk.app_sync_cdk_stack import AppSyncCdkStack


app = core.App()
AppSyncCdkStack(app, "AppSyncGraphQLDynamoDBExample")

app.synth()
Empty file.
184 changes: 184 additions & 0 deletions python/appsync-graphql-dynamodb/app_sync_cdk/app_sync_cdk_stack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
from aws_cdk import core
from aws_cdk.aws_appsync import (
CfnGraphQLSchema,
CfnGraphQLApi,
CfnApiKey,
CfnDataSource,
CfnResolver
)
from aws_cdk.aws_dynamodb import (
Table,
Attribute,
AttributeType,
StreamViewType,
BillingMode
)
from aws_cdk.aws_iam import (
Role,
ServicePrincipal,
ManagedPolicy
)


class AppSyncCdkStack(core.Stack):
zanhsieh marked this conversation as resolved.
Show resolved Hide resolved

def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)

table_name = 'items'

items_graphql_api = CfnGraphQLApi(
self, 'ItemsApi',
name='items-api',
authentication_type='API_KEY'
)

CfnApiKey(
self, 'ItemsApiKey',
api_id=items_graphql_api.attr_api_id
)

api_schema = CfnGraphQLSchema(
self, 'ItemsSchema',
api_id=items_graphql_api.attr_api_id,
definition=f"""\
type {table_name} {{
{table_name}Id: ID!
name: String
}}
type Paginated{table_name} {{
items: [{table_name}!]!
nextToken: String
}}
type Query {{
all(limit: Int, nextToken: String): Paginated{table_name}!
getOne({table_name}Id: ID!): {table_name}
}}
type Mutation {{
save(name: String!): {table_name}
delete({table_name}Id: ID!): {table_name}
}}
type Schema {{
query: Query
mutation: Mutation
}}"""
)

items_table = Table(
self, 'ItemsTable',
table_name=table_name,
partition_key=Attribute(
name=f'{table_name}Id',
type=AttributeType.STRING
),
billing_mode=BillingMode.PAY_PER_REQUEST,
stream=StreamViewType.NEW_IMAGE,

# The default removal policy is RETAIN, which means that cdk
# destroy will not attempt to delete the new table, and it will
# remain in your account until manually deleted. By setting the
# policy to DESTROY, cdk destroy will delete the table (even if it
# has data in it)
removal_policy=core.RemovalPolicy.DESTROY # NOT recommended for production code
)

items_table_role = Role(
self, 'ItemsDynamoDBRole',
assumed_by=ServicePrincipal('appsync.amazonaws.com')
)

items_table_role.add_managed_policy(
ManagedPolicy.from_aws_managed_policy_name(
'arn:aws:iam::aws:policy/AmazonDynamoDBFullAccess'
)
)

data_source = CfnDataSource(
self, 'ItemsDataSource',
api_id=items_graphql_api.attr_api_id,
name='ItemsDynamoDataSource',
zanhsieh marked this conversation as resolved.
Show resolved Hide resolved
type='AMAZON_DYNAMODB',
dynamo_db_config=CfnDataSource.DynamoDBConfigProperty(
table_name=items_table.table_name,
aws_region=self.region
),
service_role_arn=items_table_role.role_arn
)

get_one_resolver = CfnResolver(
self, 'GetOneQueryResolver',
api_id=items_graphql_api.attr_api_id,
type_name='Query',
field_name='getOne',
data_source_name=data_source.name,
request_mapping_template=f"""\
{{
"version": "2017-02-28",
"operation": "GetItem",
"key": {{
"{table_name}Id": $util.dynamodb.toDynamoDBJson($ctx.args.{table_name}Id)
}}
}}""",
response_mapping_template="$util.toJson($ctx.result)"
)

get_one_resolver.add_depends_on(api_schema)

get_all_resolver = CfnResolver(
self, 'GetAllQueryResolver',
api_id=items_graphql_api.attr_api_id,
type_name='Query',
field_name='all',
data_source_name=data_source.name,
request_mapping_template=f"""\
{{
"version": "2017-02-28",
"operation": "Scan",
"limit": $util.defaultIfNull($ctx.args.limit, 20),
"nextToken": $util.toJson($util.defaultIfNullOrEmpty($ctx.args.nextToken, null))
}}""",
response_mapping_template="$util.toJson($ctx.result)"
)

get_all_resolver.add_depends_on(api_schema)

save_resolver = CfnResolver(
self, 'SaveMutationResolver',
api_id=items_graphql_api.attr_api_id,
type_name='Mutation',
field_name='save',
data_source_name=data_source.name,
request_mapping_template=f"""\
{{
"version": "2017-02-28",
"operation": "PutItem",
"key": {{
"{table_name}Id": {{ "S": "$util.autoId()" }}
}},
"attributeValues": {{
"name": $util.dynamodb.toDynamoDBJson($ctx.args.name)
}}
}}""",
response_mapping_template="$util.toJson($ctx.result)"
)

save_resolver.add_depends_on(api_schema)

delete_resolver = CfnResolver(
self, 'DeleteMutationResolver',
api_id=items_graphql_api.attr_api_id,
type_name='Mutation',
field_name='delete',
data_source_name=data_source.name,
request_mapping_template=f"""\
{{
"version": "2017-02-28",
"operation": "DeleteItem",
"key": {{
"{table_name}Id": $util.dynamodb.toDynamoDBJson($ctx.args.{table_name}Id)
}}
}}""",
response_mapping_template="$util.toJson($ctx.result)"
)

delete_resolver.add_depends_on(api_schema)
3 changes: 3 additions & 0 deletions python/appsync-graphql-dynamodb/cdk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"app": "python3 app.py"
}
4 changes: 4 additions & 0 deletions python/appsync-graphql-dynamodb/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
aws_cdk.core
aws_cdk.aws_appsync
aws_cdk.aws_dynamodb
aws_cdk.aws_iam