Front-End Web & Mobile
AWS AppSync pipeline resolvers and functions now support additional array methods and arrow functions
AWS AppSync is a managed service that makes it easy to build scalable APIs that connect applications to data. Developers use AppSync every day to build GraphQL APIs that interact with data sources like Amazon DynamoDB, AWS Lambda, and HTTP APIs. With AppSync, developers can write their resolvers using JavaScript, and run their code on AppSync’s APPSYNC_JS runtime.
Today, we are adding functionality to the APPSYNC_JS runtime with support for the following higher-order functions on arrays:
- Array.prototype.forEach
- Array.prototype.map
- Array.prototype.flatMap
- Array.prototype.filter
- Array.prototype.reduce
- Array.prototype.reduceRight
- Array.prototype.find
- Array.prototype.some
- Array.prototype.every
- Array.prototype.findIndex
- Array.prototype.findLast
- Array.prototype.findLastIndex
With this update, APPSYNC_JS now also supports arrow functions, which can be defined at any level within our resolver or function code. This allows to write code like this:
or
and
With arrow functions and the new Array function support, we can now compactly write our business logic. This makes it easier to write code to solve common problems with arrays and objects, and we can also use them to write more complex functionality and utilities. We give a couple of examples below.
Writing a library to update an item in a DynamoDB table
Updating an item is a common task when working with data in Amazon DynamoDB tables. We can use the newly introduced Array functions to write a helper that creates an UpdateItem request. Given the schema
We can define the following APPSYNC_JS function to attach to our resolver. We define an update
function that uses Array.reduce
to iterate over the list of values and create an UpdateItem
request.
After attaching the function to our pipeline resolver for the updatedUser
mutation, we can update the user’s name and delete their status with this operation:
Handling single table design queries
AppSync allows to define DynamoDB datasources that use a single-table design or a multi-table approach. In a single-table design, different data types are stored in one DynamoDB table. With single-table design, we can retrieve all of our items in a single query from a single table. For example, we can update our schema to retrieve a user and their orders in a single request. First, we update our schema as show below. A user has orders, and each order is made up of zero or more items.
The data is stored in a DynamoDB table using a composite key:
- the partition key
id
- a sort key called
SK
To fetch data about a user and their orders and items, we query the table for any items where the partition key is equal to the user id. Each item in the table has a __typename
attributes that specifies the item type (e.g.: User
, Order
, or Item
).
We attach the following APPSYNC_JS function to the getUserAndOrderDetails
resolver:
In the response, we find the User
item using the Array.find
method. We then iterate through the items using Array.forEach
to find each order and order item. We then associate each item with its order. We can now use a query to get our data in the appropriate shape for clients:
Conclusion
In this post, we went over the new Array functions and arrow function notation introduced in APPSYNC_JS. We saw how these updated features allows to easily work with arrays and objects. We also saw how you can use them to solve common complex problems when working with data in resolvers. You can get started today in any region when AppSync is available, by reviewing APPSYNC_JS’s supported built-in objects and functions, and by reading the DynamoDB JavaScript resolvers tutorial for AppSync.