DEV Community

loading...

Discussion on: Direct Lambda Resolvers with AWS Amplify and AppSync

Collapse
andthensumm profile image
Matt Marks 🐣 Author

Thank you Shawn for catching this, it's a treat just to have you actually use this code!

Collapse
swyx profile image
swyx

not at all lol, i barely know what i'm doing and was following along just to try to learn about this new feature 😅

i think the struggle i had was how to use direct lambda resolvers in mutations not just queries. i couldnt figure out how to do it. maybe worth a followup post?

Thread Thread
andthensumm profile image
Matt Marks 🐣 Author

Pfffh, you got it. I'll whip something up for this week.

Thread Thread
nxtra profile image
Nxtra

Did you guys manage to figure this out?

Thread Thread
andthensumm profile image
Matt Marks 🐣 Author • Edited

@nxtra , in order to do this for a mutation, you'd set the typeName: Mutation and fieldName: yourMutationName. Query, Mutation and Subscription are really just a Type like Todo.

The one gotcha is that you can't create a resolver on a mutation that already has one. Example being createToDo. Amplify has already created a resolver for it inside CloudFormation. You'll need to either to add @model(mutations: null) so you can override it or create a custom mutation. I prefer the custom mutation because then I still have access to the autogenerated vtl from Amplify. There are times where the authorization rules that are generated in the response.vtl can still be useful.

  "MutationyourMutationNameLambdaResolver": {
  "Type": "AWS::AppSync::Resolver",
  "Properties": {
    "ApiId": {
        "Ref": "AppSyncApiId"
    },
    "DataSourceName": "DirectLambdaResolverLambdaDataSource",
    "TypeName": "Mutation",
    "FieldName": "yourMutationName"
  },

  "DependsOn": "DirectLambdaResolverLambdaDataSource"
}
Enter fullscreen mode Exit fullscreen mode
Thread Thread
nxtra profile image
Nxtra

Thanks for the info! I didn't realize yet that indeed a resolver with that name already exists if you name it like that.
Until now I've been making custom Resolvers with lambda functions as pipeline resolvers. I'll try to convert one to a "no-more-vtl" version using your guide.