How do I insert an optional field as null using AppSync Resolvers and Aurora?


I have an optional String field, notes, that is sometimes empty. If it’s empty I want to insert null, otherwise I want to insert the string.

Here is my resolver –


With this graphql

I get –


when I really want null without the quotes.

And with this graphql –

I get –

which is what I want.

How do I get a quoteless null and a quoted string into the same field?


TL;DR you should use $util.toJson() to print the $context.arguments.notes correctly. Replace your $notes assignment with


The reason is VTL prints whatever the toString() method returns and your call to
$util.defaultIfNullOrEmpty($context.arguments.notes, 'null') will return the string "null", which will be printed as "null".

If you replace with $util.defaultIfNullOrEmpty($context.arguments.notes, null) then it will return a null string. However, VTL will print $notes because that is the way it handles null references. In order to print null, which is the valid JSON representation of null, we have to serialize it to JSON. So the correct statement is:

Full test:

I’m assuming you started with the RDS sample provided in the AWS AppSync console and modified it. To reproduce, I updated the content field in the Schema to be nullable:

and I modified the posts table schema so content can also be null there: (inside the Lambda function)

This is the request template for the createPost mutation:

and response template:

The following query:




Leave a Reply