AWS AppSync Velocity Templates Guide

Gerard Sans
Mar 28 · 6 min read
Geometric Shapes / 180927 by Sasj

What is AWS AppSync?

AppSync Resolvers

Velocity Templates

Hello World Example

type Query {
helloWorld: String
}
AppSync Resolver (no data source)
query ExampleQuery {
helloWorld
}
#set( $var = "world" )
{
"version": "2018–05–29", ## behaviour for null and errors
"payload": "hello ${var}" ## $var in formal notation
}

VTL directives are not rendered to the output but may render results. Eg: #set, #if, #else, #end, #foreach

{
"version": "2018–05–29",
"payload": "Hello world"
}
#return( $context.result )
{ 
"data" : {
"helloWorld": "Hello world"
}
}
Overview AppSync Resolver (no data source)

Passing data between templates

query ExampleQuery {
helloWorld
alias: helloWorld
}

Tip: save some keystrokes using $ctx instead of $context

$util.qr($ctx.stash.put("t0", $util.time.nowEpochMilliSeconds()))
{
"version": "2018–05–29",
"payload": "Hello world"
}
#set( $t1 = $util.time.nowEpochMilliSeconds() )
#set( $total = $t1 - $ctx.stash.t0 )
#return( "${ctx.result} [${total} ms] [$ctx.stash.t0..${t1}]" )
{
"data": {
"helloWorld": "Hello world [7 ms] [1553389693379..1553389693386]",
"alias": "Hello world [20 ms] [1553389693382..1553389693402]"
}
{
"data": {
"a0": "Hello world [26 ms] [1553389576785..1553389576811]",
"a1": "Hello world [41 ms] [1553389576788..1553389576829]",
"a2": "Hello world [47 ms] [1553389576802..1553389576849]",
"a3": "Hello world [59 ms] [1553389576805..1553389576864]",
"a4": "Hello world [81 ms] [1553389576808..1553389576889]",
"a5": "Hello world [25 ms] [1553389576785..1553389576810]",
"a6": "Hello world [41 ms] [1553389576788..1553389576829]",
"a7": "Hello world [41 ms] [1553389576802..1553389576843]",
"a8": "Hello world [52 ms] [1553389576805..1553389576857]",
"a9": "Hello world [75 ms] [1553389576808..1553389576883]",
"a10": "Hello world [3 ms] [1553389576785..1553389576788]"
}
}
AppSync Resolver using stash (simplified)

Conclusion


Thanks for reading

Looking for speakers? Look no more

Nader Dabit (#1), Kurt Kemple (#2), Dennis Hills (#3), Gerard Sans (#4)

Thanks to Antoine Boyer and Nader Dabit

Gerard Sans

Written by

Developer Advocate @AWSCloud | Just be AWSome | MC Speaker Trainer Community Leader | Views are my own | @fullstackcon @ReactiveConf @ngcruise @UphillConf UK ☂

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade