Auto populate field in create mutation with Velocity resolver in AppSync Apmplify

49 Views Asked by At

In my AWS amplify app, I have 3 dynamodb tables. User, Post and Comment. Post belongs to a User. Commnet belongs to a Post. With velocity resolvers, in createComment mutation, I want to auto populate the postCommentsId (foreign key) field of Comments table by first querying the Post table with Post.owner (I have the ID of the logged in user - $ctx.identity.username) to get the current user's Post.id (Assuming User only has one Post). I want to create a resolver as a part of a pipeline and slot it in.

If I extend an existing resolver (say preDataLoad) from the Comment stack (as explained here), the $ctx.stash.tableName will be Comment-xxxxx-dev. How can I slot a VTL resolver which has a different data source (Post table)

What I tried: Extending Mutation.createComment.init.1.req.vtl resolvers and hardcoding $util.qr($ctx.stash.defaultValues.put("postCommentsId", "123xyz")). It did insert 123xyz as the value of postCommentsId

0

There are 0 best solutions below