Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In AWS Resolver Mapping Template, is there any method for removing a field from an object?

I'm writing code for my GraphQL resolvers in AWS AppSync with resolver mapping template.

I know that there is a put mehtod that I can use for add a field to input object or any other object. Like this (for example):

$util.qr($name.put("firstName", "$ctx.args.input.firstName"))

But now I want to remove a field from an object, for example, the input object. Is there any mehtod similar to the put method but for removing a field. something like:

$util.qr($ctx.args.input.remove("firstName"))

I am new to AWS and DynamoDB and AppSync.( you can consider me as an absolute beginner. )

like image 812
Orilious Avatar asked Dec 04 '25 12:12

Orilious


2 Answers

Use foreach and make a new array.

#set($newInput={})

#foreach ($key in $ctx.args.input.keySet())
  #if($key!="firstName")
     $util.qr($newInput.put($key, $ctx.args.input.get($key)))
  #end
#end
like image 167
uchar Avatar answered Dec 07 '25 13:12

uchar


Yes, generally you can use $myObject.remove("myKey") on objects that you create in a mapping template, however, I will add the disclaimer that this will not always work on objects in the $ctx as some parts are immutable. AppSync bundles utility methods that make dealing with objects in mapping templates easier (e.g. making copies of objects). This functionality is actually tied to that of Apache Velocity so you can read more about how it works in those docs.

like image 37
mparis Avatar answered Dec 07 '25 15:12

mparis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!