# MappingTemplate

MappingTemplates for AppSync resolvers.

- Source: [`aws-appsync/lib/mapping-template.ts`](https://github.com/aws/aws-cdk/blob/v2.275.0/packages/aws-cdk-lib/aws-appsync/lib/mapping-template.ts#L7)
- Type last changed: v2.60.0 · 12 Jan 2023

## Example

```ts
// Create username and password secret for DB Cluster
const secret = new rds.DatabaseSecret(this, 'AuroraSecret', {
  username: 'clusteradmin',
});

// The VPC to place the cluster in
const vpc = new ec2.Vpc(this, 'AuroraVpc');

// Create the serverless cluster, provide all values needed to customise the database.
const cluster = new rds.ServerlessCluster(this, 'AuroraCluster', {
  engine: rds.DatabaseClusterEngine.AURORA_MYSQL,
  vpc,
  credentials: { username: 'clusteradmin' },
  clusterIdentifier: 'db-endpoint-test',
  defaultDatabaseName: 'demos',
});

// Build a data source for AppSync to access the database.
declare const api: appsync.GraphqlApi;
const rdsDS = api.addRdsDataSource('rds', cluster, secret, 'demos');

// Set up a resolver for an RDS query.
rdsDS.createResolver('QueryGetDemosRdsResolver', {
  typeName: 'Query',
  fieldName: 'getDemosRds',
  requestMappingTemplate: appsync.MappingTemplate.fromString(`
  {
    "version": "2018-05-29",
    "statements": [
      "SELECT * FROM demos"
    ]
  }
  `),
  responseMappingTemplate: appsync.MappingTemplate.fromString(`
    $utils.toJson($utils.rds.toJsonObject($ctx.result)[0])
  `),
});

// Set up a resolver for an RDS mutation.
rdsDS.createResolver('MutationAddDemoRdsResolver', {
  typeName: 'Mutation',
  fieldName: 'addDemoRds',
  requestMappingTemplate: appsync.MappingTemplate.fromString(`
  {
    "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)
    }
  }
  `),
  responseMappingTemplate: appsync.MappingTemplate.fromString(`
    $utils.toJson($utils.rds.toJsonObject($ctx.result)[1][0])
  `),
});
```

## Initializer

```ts
new MappingTemplate()
```

## Methods

- `dynamoDbDeleteItem(keyName: string, idArg: string): MappingTemplate // chainable` (static) — Mapping template to delete a single item from a DynamoDB table.

  **Parameters**

  - `keyName` — the name of the hash key field.
  - `idArg` — the name of the Mutation argument.

- `dynamoDbGetItem(keyName: string, idArg: string, consistentRead?: boolean): MappingTemplate // chainable` (static) — Mapping template to get a single item from a DynamoDB table.

  **Parameters**

  - `keyName` — the name of the hash key field.
  - `idArg` — the name of the Query argument.

- `dynamoDbPutItem(key: PrimaryKey, values: AttributeValues): MappingTemplate // chainable` (static) — Mapping template to save a single item to a DynamoDB table.

  **Parameters**

  - `key` — the assigment of Mutation values to the primary key.
  - `values` — the assignment of Mutation values to the table attributes.

- `dynamoDbQuery(cond: KeyCondition, indexName?: string, consistentRead?: boolean): MappingTemplate // chainable` (static) — Mapping template to query a set of items from a DynamoDB table.

  **Parameters**

  - `cond` — the key condition for the query.

- `dynamoDbResultItem(): MappingTemplate // chainable` (static) — Mapping template for a single result item from DynamoDB.

- `dynamoDbResultList(): MappingTemplate // chainable` (static) — Mapping template for a result list from DynamoDB.

- `dynamoDbScanTable(consistentRead?: boolean): MappingTemplate // chainable` (static) — Mapping template to scan a DynamoDB table to fetch all entries.

- `fromFile(fileName: string): MappingTemplate // chainable` (static) — Create a mapping template from the given file.

- `fromString(template: string): MappingTemplate // chainable` (static) — Create a mapping template from the given string.

- `lambdaRequest(payload?: string, operation?: string): MappingTemplate // chainable` (static) — Mapping template to invoke a Lambda function.

  **Parameters**

  - `payload` — the VTL template snippet of the payload to send to the lambda.
  - `operation` — the type of operation AppSync should perform on the data source.

- `lambdaResult(): MappingTemplate // chainable` (static) — Mapping template to return the Lambda result to the caller.

- `renderTemplate(): string` — this is called to render the mapping template to a VTL string.
