I used AWS Amplify to create an GraphQL API. In DynamoDB the fields createdAt, updatedAt and owner are created automatically. Out of the box I have no way of getting the values for this fields. Now when I add those fields to the annotated schema, I will be able to get the values but the everybody with write-permissions can just overwrite them which is annoying for the time fields and a security risk for the owner field.
So how do I get those values?
This is an AWS-Amplify specific question. It's not about how to do this with generic GraphQL. It's very specifically about how to do this with AWS-Amplify's API module and their (sadly very limited) directives (https://aws-amplify.github.io/docs/js/api#using-graphql-transformers).
You should override create resolver like this:
Instead of:
## [Start] Prepare DynamoDB PutItem Request. **
$util.qr($context.args.input.put("createdAt", $util.defaultIfNull($ctx.args.input.createdAt, $util.time.nowISO8601())))
$util.qr($context.args.input.put("updatedAt", $util.defaultIfNull($ctx.args.input.updatedAt, $util.time.nowISO8601())))
You should:
## [Start] Prepare DynamoDB PutItem Request. **
$util.qr($context.args.input.put("createdAt", $util.time.nowISO8601()))
$util.qr($context.args.input.put("updatedAt", $util.time.nowISO8601()))
In update resolver
Instead of:
## Automatically set the updatedAt timestamp. **
$util.qr($context.args.input.put("updatedAt", $util.defaultIfNull($ctx.args.input.updatedAt, $util.time.nowISO8601())))
You should:
## Automatically set the updatedAt timestamp. **
$util.qr($context.args.input.put("updatedAt", $util.time.nowISO8601()))
aws-amplify-overriding-auto-generated
P.S. Place the new resolvers in /resolvers folder. Not in /build/resolvers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With