Leo Jiang
Leo Jiang

Reputation: 26173

Prepend custom code to an AWS Amplify resolver?

I just started trying Amplify. Apparently, to do backend input validation, you're supposed to create a custom resolver such as backend/api/projectname/resolvers/mutation.createTodo.req.vtl. Then, I put the following inside:

#if( $ctx.args.input.name.length() > 10 )
    $util.error("Value for input field `CreateTodoInput.name` cannot exceed 10 characters.")
#end

If the input length is greater than 10 characters, then I get the error message as expected. However, if the input is less than 10 characters, I get Template transformation yielded an empty response..

I'm assuming this is because I replaced the entire resolver instead of prepending to it. How can I prepend my custom validation to the resolver that Amplify auto-generates?

Upvotes: 1

Views: 601

Answers (1)

dfranca
dfranca

Reputation: 5322

You can have a look on generated resolver, and then modify it from there.

the generated resolver will be at amplify/backend/api/projectname/build/resolvers

Then copy it to the file you mentioned in the question, and modify it accordingly

This article has more details on how to override the resolver.

You can keep most of the logic, and just add your code on it.

Upvotes: 0

Related Questions