Introduction:

AppSync is a powerful service provided by AWS that allows you to build scalable and real-time applications with GraphQL. In this article, we’ll explore how to use AppSync and VTL (Velocity Template Language) to create movie lists in a serverless environment.

Understanding the GraphQL Schema:

The first step is to define the GraphQL schema that describes the structure of our movie lists. The schema includes the createMovieList mutation and the input types createMovieListInput, movieListType, and movieListCategory. These types define the required input fields and the available enum values for the movie lists.

createMovieList(input: createMovieListInput): statusItems
input createMovieListInput {
	movieListID: String!
	movieListType: movieListType!
	movieListCategory: movieListCategory!
	name: String!
	description: String
	createdByID: String!
	createdByHandle: String!
}
enum movieListType {
	User
	Editorial
}
enum movieListCategory {
	Movie
	Person
}
type statusItems {
	statusCode: String
	statusMessage: String
}

createMovieList(input: createMovieListInput): statusItems

Schema Explanation : 

1. `createMovieList` Mutation:

   -> This mutation allows you to create a new movie list.

   ->Input: It takes an argument `input` of type `createMovieListInput`, which contains various fields to define the properties of the movie list you want to create.

   ->Output: It returns a `statusItems` object, which contains `statusCode` and `statusMessage`, indicating the status of the mutation operation.

2. `createMovieListInput` Input Type:

   ->This is an input object type that defines the structure of the data that you need to provide when creating a movie list.

   ->Fields:

   ->`movieListID`: A mandatory field of type `String`. It represents the ID of the movie list and must be provided when creating a new list.

   ->`movieListType`: A mandatory field of type `movieListType`. It represents the type of the movie list, and you can only choose from the `movieListType` enum (either `User` or `Editorial`).

   ->`movieListCategory`: A mandatory field of type `movieListCategory`. It represents the category of the movie list, and you can only choose from the `movieListCategory` enum (either `Movie` or `Person`).

   ->`name`: A mandatory field of type `String`. It represents the name of the movie list and must be provided when creating a new list.

   ->`description`: An optional field of type `String`. It represents the description of the movie list, and you can provide additional details about the list if needed.

   ->`createdByID`: A mandatory field of type `String`. It represents the ID of the user who is creating the movie list, and you must provide this information during the creation.

   ->`createdByHandle`: A mandatory field of type `String`. It represents the handle or username of the user who is creating the movie list, and you must provide this information during the creation.

3. Enums:

  ->Enums are a special type in GraphQL that defines a set of allowed values for a field. In this schema, there are two enums defined: `movieListType` and `movieListCategory`.

  -> `movieListType`: This enum defines two possible values – `User` and `Editorial`. When creating a movie list, you can choose one of these values to specify the type of the list.

  ->`movieListCategory`: This enum defines two possible values – `Movie` and `Person`. When creating a movie list, you can choose one of these values to specify the category of the list.

4. `statusItems` Type:

  ->This type is used to represent the status of a mutation operation.

  ->Fields:

  ->`statusCode`: A field of type `String`. It represents the status code of the operation, which could be a success or an error code.

   ->`statusMessage`: A field of type `String`. It represents a descriptive message associated with the status code, explaining the result of the operation.

In summary, the schema defines a mutation called `createMovieList`, which takes a set of mandatory fields and enums as input to create a new movie list. The `statusItems` type is used to provide feedback on the success or failure of the mutation operation. The schema ensures that the required data is provided during the creation of a movie list and that the selected options for `movieListType` and `movieListCategory` match the allowed values specified in the enums.

Request Mapping Template:

The request mapping template is responsible for transforming 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 first set the current time using the $util.time.nowEpochSeconds() function. Then, we add the movieListTypeAndCategory field to the input by concatenating movieListType and movieListCategory using the put function. Finally, we construct the DynamoDB operation and key attributes based on the input values.

#set( $time = $util.time.nowEpochSeconds() )
$util.qr($ctx.args.input.put("movieListTypeAndCategory","$ctx.args.input.movieListType#$ctx.args.input.movieListCategory"))
{
"operation" : "PutItem",
"key" : {
"PK": $util.dynamodb.toDynamoDBJson("movieLists"),
"SK": $util.dynamodb.toDynamoDBJson("movieList#$ctx.args.input.movieListID"),
"movieListID": $util.dynamodb.toDynamoDBJson("movieList#$ctx.args.input.movieListID"),
"movieListCreatedTime": $util.dynamodb.toDynamoDBJson($time),
},
"attributeValues" : $util.dynamodb.toMapValuesJson($ctx.args.input)
}

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 data operation, the VTL code checks for the presence of an error and throws it using the $util.error() function. Otherwise, it 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.

Conclusion:

#if($ctx.error)
$util.error($ctx.error.message, $ctx.error.type)
#end
#set( $status = {} )
$util.qr($status.put("statusCode","M200"))
$util.qr($status.put("statusMessage","Created Movie List Successfully"))
$util.toJson($status)

Here, we explored the process of creating 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 leverage the power of AppSync and VTL to build robust and scalable applications with custom data operations.

Explore Next Articles in this series by clicking here.

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

Deleting Movie Lists with AppSync GraphQL and VTL(Part -3)

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