How would I autofill a field in an aws appsync schema?

199 views Asked by At

I'd like to autofill a "userId" field when an object of this type is created.

type FriendRequest @model
@key(fields: ["userId", "receiver"])
{
  userId: ID!
  receiver: ID!
}

How would this be done? Would I need an @function directive on the userId field? What would that function look like?

1

There are 1 answers

0
Sean On

Found the answer. Find the velocity resolvers in amplify\backend\api<your api name>\build\resolvers and move the mutations you want to edit into amplify\backend\api<your api name>\resolvers.

To autofill a field I used this line, which is based off the line of code used to autofill the "createdAt" and "updatedAt" fields for appsync api objects:

$util.qr($context.args.input.put("your field name here", $util.defaultIfNull($ctx.args.input.your field name here, [your value here])))

In my case I used $context.identity.sub to get the user's id, which I learned from this doc.