AppSync: how to compare strings lexicographically in VTL
Tags: appsync, aws, serverless
Published: 2020-05-02 19:06:00

---
title: AppSync: how to compare strings lexicographically in VTL
published: true
date: 2020-05-01 22:41:24 UTC
tags: AppSync,AWS,Serverless
canonical_url: https://theburningmonk.com/2020/05/appsync-how-to-compare-strings-lexicographically-in-vtl/
---

I had been working on a client project where I built the GraphQL backend for a new social network using primarily AppSync, Lambda and DynamoDB.

One of the features of the app was to support private messaging between two users. To allow either user to fetch their chat messages in chronological descending order, I modelled the chat\_messages DynamoDB table as below:

![](https://theburningmonk.com/wp-content/uploads/2020/05/img_5eaca3a303349.png)

where `userIds` is the HASH key, and timestamp is the `RANGE` key.

Here, `userIds` is a composite key made up of the two users’ IDs, arrange in lexicographical order. If you know the two users’ IDs, you can fetch the chat messages using a DynamoDB `Query` operation.

So in the VTL template, I had something like this:

![](https://theburningmonk.com/wp-content/uploads/2020/05/img_5eaca4b375aab.png)

However, this didn’t work.

Given the following input:

![](https://theburningmonk.com/wp-content/uploads/2020/05/img_5eaca4c401124.png)

The `$userIds` variable would be interpreted as `35c1f714–46c1–4ccb-87fc-933da626ce6a_08371580–48e2–43e7–9b1c-75804b9e7e12`, where the user IDs are arranged in the wrong order. What’s going on?

If you work with Java everyday then the problem might be obvious to you, that to compare strings lexicographically you need to use the `compareTo` method on the first string. And since VTL is implemented in Java, that is also the case here.

Instead, the right way to do it is this:

![](https://theburningmonk.com/wp-content/uploads/2020/05/img_5eaca50fa3b85.png)

I have found the [VTL Reference](https://velocity.apache.org/engine/1.7/vtl-reference.html) page very handy while working with VTL. But unfortunately, lexicographical string comparison was not mentioned in the VTL docs. Without helpful folks like [Richard](https://twitter.com/rchrdbyd) and [Franck](https://twitter.com/Franck_chester) I would have easily spent hours banging my head against this problem!

{% twitter 1254858236223578112 %}

{% twitter 1254853625005969409 %} 

I hope this post helps you in a similar way should you run into the same problem, as my way of passing it forward.

---

 ![](https://theburningmonk.com/wp-content/uploads/2019/10/img_5dab0c477fc72.png)

Hi, my name is **Yan Cui**. I’m an **[AWS Serverless Hero](https://aws.amazon.com/developer/community/heroes/yan-cui/)** and the author of [**Production-Ready Serverless**](https://bit.ly/production-ready-serverless). I specialise in rapidly transitioning teams to serverless and building production-ready services on AWS.

Are you struggling with serverless or need guidance on best practices? Do you want someone to review your architecture and help you avoid costly mistakes down the line? Whatever the case, I’m here to help.

You can contact me via [Email](mailto:theburningmonk.com), [Twitter](https://twitter.com/theburningmonk) and [LinkedIn](https://www.linkedin.com/in/theburningmonk/).

[Hire me.](https://theburningmonk.com/hire-me)

The post [AppSync: how to compare strings lexicographically in VTL](https://theburningmonk.com/2020/05/appsync-how-to-compare-strings-lexicographically-in-vtl/) appeared first on [theburningmonk.com](https://theburningmonk.com).

 ![](http://feeds.feedburner.com/~r/theburningmonk/~4/ULrJkqm6TZo)