Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get createdAt, updatedAt and owner fields from AWS Amplify generated GraphQL documents?

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).

like image 348
Philip Claren Avatar asked Oct 16 '22 08:10

Philip Claren


1 Answers

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

like image 191
Selcuk Yilmaz Avatar answered Oct 21 '22 03:10

Selcuk Yilmaz