0
votes

For complex field resolver, I know @function directive is the way to go.

But how about those very simple computed fields. @function directive with lambda is a little too much. I see prisma has inline javascript function supported. see the answer of similar quest.

Not sure whether aws-amplify graphql support inline function.

1
You can use VTL custom resolvers, but there is a lot of boilerplate: docs.amplify.aws/cli/graphql-transformer/… - dfranca
It's true, AppSync is missing a little something, JavaScript functions. VTL is kinda like Java. Possibly write the function in Java VTL. Can you explain how short the inline function is? I don't know. I like AppSync. Efforts to use AppSync shouldn't go to waste. - starpebble

1 Answers

1
votes

You can override the default VTL resolver for the field, just adding the logic you want.

This article goes into detail on a simple use case (similar to yours I think)

Just a few steps

  • Add the field to your schema
  • Build your API
  • Look for the auto-generated resolver (amplify/backend/api/client/build/resolvers) The naming convention is straight-forward.
  • Copy it to amplify/backend/api/client/resolvers
  • Change it as needed
  • Push your changes to Amplify

In the article he has just added a new set item

## [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()))
# The next line was added
$util.qr($context.args.input.put("active", false))

AWS has some tutorials over VTL that you might want to take a look.

And Amplify has more docs on custom resolvers using VTL