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)
Deleting Movie Lists with AppSync GraphQL and VTL(Part -3)
Introduction:
AppSync, an AWS service, allows you to build scalable and real-time applications using GraphQL. In this article, we will explore how to leverage AppSync and the AppSync JavaScript library to fetch movie lists based on different filters and retrieve the results.
Understanding the GraphQL Schema:
To begin, let’s understand the GraphQL schema that describes the movie lists and the available filter options. The getMovieLists query accepts parameters such as movieListType, movieListCategory, filterBy, limit, and nextToken to control the fetching of movie lists. The enum types movieListType, movieListCategory, and movieListFilter define the possible values for these filters.
Schema Explanation:
1. Query:
– `getMovieLists`: This is a query to fetch a list of movie items based on certain criteria.
– Arguments:
– `movieListType`: A filter to get movie lists of a specific type (Enum: `movieListType`).
– `movieListCategory`: A filter to get movie lists of a specific category (Enum: `movieListCategory`).
– `filterBy`: A mandatory field of type `movieListFilter` that determines the type of filtering to be applied.
– `limit`: An optional integer value to limit the number of results.
– `nextToken`: An optional string to paginate through the results.
– Return Type:
– `movieList`: An object type with two fields.
– `items`: A list of `movieListItem` objects (array of movie items).
– `nextToken`: A string representing the token for pagination.
2. Enums:
– `movieListType`: An enum type representing the different types of movie lists (`User` and `Editorial`).
– `movieListCategory`: An enum type representing the different categories of movie lists (`Movie` and `Person`).
– `movieListFilter`: An enum type representing the various options to filter movie lists (`all`, `movieListType`, `movieListCategory`, `movieListTypeAndCategory`).
3. Object Types:
– `movieList`: An object type representing a list of movie items.
– Fields:
– `items`: A list of `movieListItem` objects (array of movie items).
– `nextToken`: A string representing the token for pagination.
– `movieListItem`: An object type representing an individual movie item.
– Fields:
– `movieListTypeAndCategory`: A string field representing the combination of movie list type and category.
– `PK`: A string field representing a unique identifier for the movie list item.
– `SK`: A string field representing a sort key for the movie list item.
– `name`: A string field representing the name of the movie.
– `description`: A string field representing the description of the movie.
– `movieListID`: A string field representing the ID of the movie list.
– `movieListType`: A field of type `movieListType`, representing the type of the movie list.
– `movieListCategory`: A field of type `movieListCategory`, representing the category of the movie list.
– `movieListCreatedTime`: An integer field representing the creation timestamp of the movie list.
– `createdByID`: A string field representing the ID of the user who created the movie list.
– `createdByHandle`: A string field representing the handle of the user who created the movie list.
– `priority`: An integer field representing the priority of the movie list.
– `createdBy`: A string field representing the name of the user who created the movie list.
In summary, the schema defines a query `getMovieLists` that retrieves a list of movie items based on various filter options such as `movieListType`, `movieListCategory`, and `filterBy`. The response is an object containing the `items` field, which holds an array of movie items (`movieListItem` objects), and the `nextToken` field for pagination. The schema also includes enums for `movieListType`, `movieListCategory`, and `movieListFilter` to define the allowed values for filtering. The `movieListItem` object type represents individual movie items with various attributes like name, description, category, etc.
Request Function:
The request function is responsible for transforming the incoming GraphQL request into a format that can be understood by the underlying data source. In the provided AppSync JavaScript code, we extract the necessary arguments such as limit, nextToken, and filterBy from the ctx.arguments object. We then use a switch statement to determine the appropriate index and query expression based on the filterBy value. Each case sets the index and constructs the queryExpression using the util.transform.toDynamoDBConditionExpression function, which converts the GraphQL filter conditions to a DynamoDB condition expression. Finally, we return an object containing the necessary parameters for the DynamoDB Query operation.
import { util } from ‘@aws-appsync/utils’;
export function request(ctx) {
var { limit, nextToken,filterBy } = ctx.arguments;
nextToken = ctx.arguments.nextToken ? ctx.arguments.nextToken : null;
var index;
var queryExpression;
switch(filterBy){
case "all":
index = "GSIMovieListsByCreatedTime";
queryExpression = util.transform.toDynamoDBConditionExpression({
PK: { eq: "movieLists" },
});
break;
case "movieListType":
index = "GSIMovieListsByTypeAndTime";
queryExpression =util.transform.toDynamoDBConditionExpression({
movieListType : { eq: ctx.arguments.movieListType },
});
break;
case "movieListCategory":
index = "GSIMovieListsByCategoryAndTime";
queryExpression = util.transform.toDynamoDBConditionExpression({
movieListCategory : { eq: ctx.arguments.movieListCategory },
});
break;
case "movieListTypeAndCategory":
index = "GSIListsByTypeCategoryAndTime";
queryExpression = util.transform.toDynamoDBConditionExpression({
movieListTypeAndCategory : { eq: `${ctx.arguments.movieListType}#${ctx.arguments.movieListCategory}` },
});
break
default:
console.log("in default case");
}
var query = JSON.parse(queryExpression);
console.log(query,"query");
return { operation: "Query", index, query, limit, nextToken,scanIndexForward : false};
}
Response Function:
The response function handles the response from the underlying data source and transforms it into the desired format for the GraphQL response. In the provided code, we simply return ctx.result, which represents the result of the DynamoDB Query operation.
export function response(ctx) {
return ctx.result
}
Conclusion:
Here, we explored the process of fetching movie lists based on different filters using AppSync GraphQL and the AppSync JavaScript library. We discussed the GraphQL schema, the request function for transforming the request, and the response function for handling the response. By understanding these components, you can utilize the capabilities of AppSync and the AppSync JavaScript library to fetch movie lists efficiently and with flexibility.
Remember to adapt the code according to your specific requirements and consider additional features such as pagination, caching, and error handling. AppSync provides a powerful and scalable platform for building GraphQL APIs, and the AppSync JavaScript library simplifies the integration process.