in python/appsync-graphql-dynamodb/app_sync_cdk/app_sync_cdk_stack.py [0:0]
def __init__(self, scope: 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=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(
'AmazonDynamoDBFullAccess'
)
)
data_source = CfnDataSource(
self, 'ItemsDataSource',
api_id=items_graphql_api.attr_api_id,
name='ItemsDynamoDataSource',
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"""\