Explore previous articles in this series by clicking here.

Creating Movie Lists with AppSync GraphQL and VTL(Part -1)

Updating Movie Lists with AppSync GraphQL and VTL(Part -2)


AppSync, an AWS service, enables you to build real-time and scalable applications using GraphQL. In this article, we will explore how to leverage AppSync and VTL (Velocity Template Language) to delete movie lists in a serverless environment.

Understanding the GraphQL Schema:

To begin, it is important to understand the GraphQL schema that describes the structure of our movie lists. The schema includes the deleteMovieList mutation and the input type deleteMovieListInput, which contains the primary key fields (PK and SK) of the movie list to be deleted.

deleteMovieList(input: deleteMovieListInput): statusItems

input deleteMovieListInput {
	PK: String!
	SK: String!

type statusItems {
	statusCode: String
	statusMessage: String

Schema Explanation:

1. `deleteMovieList` Mutation:

   This is a GraphQL mutation operation named `deleteMovieList`. Mutations are used to modify data on the server, such as creating, updating, or deleting records.

2. `input deleteMovieListInput`:

   This is an input object type called `deleteMovieListInput`. Input types are used to pass parameters to a mutation or query. In this case, the `deleteMovieList` mutation takes an input parameter of type `deleteMovieListInput`.

3. Fields of `deleteMovieListInput`:

   – `PK`: A field of type `String!`

   – `SK`: A field of type `String!`

4. `String!`:

   The exclamation mark `!` at the end of the type `String` indicates that this field is non-nullable. It means that when invoking the `deleteMovieList` mutation, the `PK` and `SK` fields must be provided, and they cannot be null. They must have valid string values.

5. `type statusItems`:

   This is an output object type called `statusItems`. Output types are used to define the shape of the data returned by a GraphQL operation.

6. Fields of `statusItems`:

   – `statusCode`: A field of type `String`

   – `statusMessage`: A field of type `String`

7. `String`:

   The `String` type represents textual data, and it can be nullable (not followed by an exclamation mark) in this case. The absence of an exclamation mark means that the `statusCode` and `statusMessage` fields can be nullable, and they may or may not be present in the response. If they are present, they will be of type `String`.

In summary, the `deleteMovieList` mutation takes an input object `deleteMovieListInput`, which requires two non-nullable string fields, `PK` and `SK`. These fields are used to identify the movie list that needs to be deleted. After executing the mutation, the response will contain a `statusItems` object with two optional string fields, `statusCode` and `statusMessage`, representing the status of the operation.

When calling the `deleteMovieList` mutation, you need to provide the `PK` and `SK` fields as input, and the server will respond with the status of the deletion process.

Request Mapping Template:

The request mapping template’s purpose is to transform the incoming GraphQL request into a format that can be understood by the underlying data source, in this case, DynamoDB. In the provided VTL code, we construct a JSON object with the operation set to “DeleteItem”. We then specify the primary key fields (PK and SK) to identify the movie list to be deleted. The $util.dynamodb.toDynamoDBJson() function is used to convert the values to the appropriate JSON format.

"operation": "DeleteItem",
"key": {
"PK": $util.dynamodb.toDynamoDBJson($ctx.args.input.PK),
"SK": $util.dynamodb.toDynamoDBJson($ctx.args.input.SK),

Response Mapping Template:

The response mapping template handles the response from the data source and transforms it into the desired GraphQL response format. If an error occurs during the deletion operation, the VTL code checks for the presence of an error and appends it to the GraphQL field error. If the deletion is successful, the code constructs the status object with the desired status code and message using the put function. Finally, it converts the status object to JSON using $util.toJson() and returns it as the GraphQL response.

$utils.appendError($ctx.error.message, $ctx.error.type)
#set($status = {})
$util.qr($status.put("statusCode", "M202"))
$util.qr($status.put("statusMessage", "Deleted Movie List Successfully"))


Here, we explored the process of deleting movie lists using AppSync GraphQL and VTL. We discussed the GraphQL schema, request mapping template, and response mapping template used in the AppSync resolver. By understanding these components, you can harness the capabilities of AppSync and VTL to delete movie lists in a serverless environment.

By following the steps outlined in this article, you can implement the given functionality of deleting movie lists in your own AppSync GraphQL service. Remember to adapt the code to fit your specific requirements and consider additional features such as input validation, authorization, and error handling.

Explore Next Articles in this series by clicking here.

Fetching Movie Lists with AppSync GraphQL and AppSync JavaScript(Part -4)