1
votes

Using AWS Amplify Transform to define a schema in "schema.graphql" which is then compiled and pushed to AWS Cloudformation. When data is created in DynamoDB a string is automatically created (defined in the automatically produced resolvers) for "createdAt" and "updatedAt". If I specify an @auth directive related to the "owner" it will put the owner field on the record. All of this is defined in the schema.graphql file.

I want something similar to the above, and want to know if I can do it in the schema.graphql transform file, or if I have to start customizing resolvers.

I simply want to record the user name that created and last modified the record. So on every record I want "createdBy" and "updatedBy". Don't need owner as the application doesn't require it - but I want to know who created it and who last updated and when it all occurred.

So before I go making this more complex than it probably is I wanted to check if there is something simple I can do to achieve this.

1

1 Answers

1
votes

I've worked this out so just updating for others. In the app/resolvers directory you put your custom resolver and enter the following text for the "type" (table) you are creating.

## [Start] Prepare DynamoDB PutItem Request. **
$util.qr($context.args.input.put("created", $util.time.nowISO8601()))
$util.qr($context.args.input.put("lastModified", $util.time.nowISO8601()))
$util.qr($context.args.input.put("createdBy", $ctx.identity.username))
$util.qr($context.args.input.put("createdById", $ctx.identity.sub))
$util.qr($context.args.input.put("createdByIP", $ctx.identity.sourceIp))
$util.qr($context.args.input.put("lastModifiedBy", $ctx.identity.username))
$util.qr($context.args.input.put("lastModifiedById", $ctx.identity.sub))
$util.qr($context.args.input.put("lastModifiedByIP", $ctx.identity.sourceIp))
$util.qr($context.args.input.put("__typename", "Assets"))