Maciej Radzikowski Profile picture
🛠️ AWS Community Builder ☁️ Doing serverless on AWS 📚 Sharing knowledge: bite-sized tips on Twitter and longer ones on https://t.co/sqFSZhgkbM

Jun 1, 2022, 15 tweets

AWS AppSync added new $utils to VTL.

Let's look at the new and existing AppSync $utils with 9 code examples!

A thread 🧵👇

VTL, the Velocity Template Language, is used to build JSONs for AppSync Resolvers. VTL allows putting logic in the Resolvers' behavior.

$util provides a set of functions with rich capabilities to enhance this logic without utilizing additional Lambda functions.

Btw, VTL is not the easiest or most developer-friendly language.

But this RFC gives us hope for the JavaScript-based Resolvers in the future:
github.com/aws/aws-appsyn…

Now, to the point - $util functions in AppSync VTL 👇

1️⃣ Debug logs - newly added (finally!)

AppSync writes logs to the CloudWatch depending on the log level set:
- ERROR: error logs only
- ALL: info and error logs

2️⃣ Generating IDs

$util.autoKsuid() is newly added, K-Sortable Unique IDentifier generator.

ULID and KSUID generate unique but sortable (by creation time) identifiers!

More about KSUID: github.com/segmentio/ksuid

3️⃣ Working with JSON

Serializing and deserializing objects to/from JSON is common with any Data Sources AppSync connects to.

4️⃣ Suppressing functions output

In VTL, Lists and Maps are Java objects.
You can use Java API to work with them.

Many Map and List methods return values that you don't want to include in the template output. Therefore, you need to discard or "suppress" them.

5️⃣ Throwing error

6️⃣ Validating data

$util.validate() function will throw an error if the condition is not fulfilled. No #if blocks are needed.

7️⃣ Default value fallback

Perfect for handling optional query arguments.

8️⃣ String conversions

Only the basics are available - uppercase, lowercase, and find and replace.

Btw, I understand that "toReplace" name was supposed to match "toUpper" and "toLower"...

9️⃣ Generating timestamps

Please use ISO 8601 wherever you can...

That's not all!

There are also helpers for working with Lists, Maps, DynamoDB, RDS, HTTP, and XML.

But this thread is long enough, so maybe next time? 😉

If you found it useful:

1. Follow me @radzikowski_m for more like this
2. Please ❤️ and 🔁 the 1st tweet above to share

Thank you!

Share this Scrolly Tale with your friends.

A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.

Keep scrolling