MappingTemplate

class aws_cdk.aws_appsync.MappingTemplate

Bases: object

MappingTemplates for AppSync resolvers.

ExampleMetadata:

infused

Example:

# Build a data source for AppSync to access the database.
# api: appsync.GraphqlApi
# Create username and password secret for DB Cluster
secret = rds.DatabaseSecret(self, "AuroraSecret",
    username="clusteradmin"
)

# The VPC to place the cluster in
vpc = ec2.Vpc(self, "AuroraVpc")

# Create the serverless cluster, provide all values needed to customise the database.
cluster = rds.ServerlessCluster(self, "AuroraCluster",
    engine=rds.DatabaseClusterEngine.AURORA_MYSQL,
    vpc=vpc,
    credentials={"username": "clusteradmin"},
    cluster_identifier="db-endpoint-test",
    default_database_name="demos"
)
rds_dS = api.add_rds_data_source("rds", cluster, secret, "demos")

# Set up a resolver for an RDS query.
rds_dS.create_resolver("QueryGetDemosRdsResolver",
    type_name="Query",
    field_name="getDemosRds",
    request_mapping_template=appsync.MappingTemplate.from_string("""
          {
            "version": "2018-05-29",
            "statements": [
              "SELECT * FROM demos"
            ]
          }
          """),
    response_mapping_template=appsync.MappingTemplate.from_string("""
            $utils.toJson($utils.rds.toJsonObject($ctx.result)[0])
          """)
)

# Set up a resolver for an RDS mutation.
rds_dS.create_resolver("MutationAddDemoRdsResolver",
    type_name="Mutation",
    field_name="addDemoRds",
    request_mapping_template=appsync.MappingTemplate.from_string("""
          {
            "version": "2018-05-29",
            "statements": [
              "INSERT INTO demos VALUES (:id, :version)",
              "SELECT * WHERE id = :id"
            ],
            "variableMap": {
              ":id": $util.toJson($util.autoId()),
              ":version": $util.toJson($ctx.args.version)
            }
          }
          """),
    response_mapping_template=appsync.MappingTemplate.from_string("""
            $utils.toJson($utils.rds.toJsonObject($ctx.result)[1][0])
          """)
)

Methods

abstract render_template()

this is called to render the mapping template to a VTL string.

Return type:

str

Static Methods

classmethod dynamo_db_delete_item(key_name, id_arg)

Mapping template to delete a single item from a DynamoDB table.

Parameters:
  • key_name (str) – the name of the hash key field.

  • id_arg (str) – the name of the Mutation argument.

Return type:

MappingTemplate

classmethod dynamo_db_get_item(key_name, id_arg, consistent_read=None)

Mapping template to get a single item from a DynamoDB table.

Parameters:
  • key_name (str) – the name of the hash key field.

  • id_arg (str) – the name of the Query argument.

  • consistent_read (Optional[bool]) –

Return type:

MappingTemplate

classmethod dynamo_db_put_item(key, values)

Mapping template to save a single item to a DynamoDB table.

Parameters:
  • key (PrimaryKey) – the assigment of Mutation values to the primary key.

  • values (AttributeValues) – the assignment of Mutation values to the table attributes.

Return type:

MappingTemplate

classmethod dynamo_db_query(cond, index_name=None, consistent_read=None)

Mapping template to query a set of items from a DynamoDB table.

Parameters:
  • cond (KeyCondition) – the key condition for the query.

  • index_name (Optional[str]) –

  • consistent_read (Optional[bool]) –

Return type:

MappingTemplate

classmethod dynamo_db_result_item()

Mapping template for a single result item from DynamoDB.

Return type:

MappingTemplate

classmethod dynamo_db_result_list()

Mapping template for a result list from DynamoDB.

Return type:

MappingTemplate

classmethod dynamo_db_scan_table(consistent_read=None)

Mapping template to scan a DynamoDB table to fetch all entries.

Parameters:

consistent_read (Optional[bool]) –

Return type:

MappingTemplate

classmethod from_file(file_name)

Create a mapping template from the given file.

Parameters:

file_name (str) –

Return type:

MappingTemplate

classmethod from_string(template)

Create a mapping template from the given string.

Parameters:

template (str) –

Return type:

MappingTemplate

classmethod lambda_request(payload=None, operation=None)

Mapping template to invoke a Lambda function.

Parameters:
  • payload (Optional[str]) – the VTL template snippet of the payload to send to the lambda. If no payload is provided all available context fields are sent to the Lambda function

  • operation (Optional[str]) – the type of operation AppSync should perform on the data source.

Return type:

MappingTemplate

classmethod lambda_result()

Mapping template to return the Lambda result to the caller.

Return type:

MappingTemplate