I have two tables: Posts and Users
In the createPost
mutation resolver, I set some default values do certain properties (think userId, createdAt timestamp, isDeleted flag etc). In addition, I would like to increment the User's numPosts
counter.
Is this possible via the standard resolvers?
If not, what's the better alternative and why?
Alternative 1) Use DynamoDB Stream and trigger lambda function when new Post record is added that increments User's numPosts
counter.
Alternative 2) Use a lambda resolver and move all logic there instead of the standard resolver.
Subscriptions in AWS AppSync are invoked as a response to a mutation. This means that you can make any data source in AWS AppSync real time by specifying a GraphQL schema directive on a mutation. The AWS Amplify client libraries automatically handle subscription connection management.
AppSync subscriptions allow you to push events to clients in real-time when a change happened. This is great for applications that show data that can change without user interaction, which is the case for almost all applications.
From the schema editor in the AWS AppSync console, on the right side, choose Attach Resolver for getPost(id:ID!): Post . Choose your Lambda data source. In the request mapping template section, choose Invoke And Forward Arguments. In the response mapping template section, choose Return Lambda Result.
You can use a BatchPut Operation to update multiple tables at the same time. Refer to Amazon DynamoDB Batch Operations guide for more information.
You need to have a resolver like
{
"version" : "2018-05-29",
"operation" : "BatchGetItem",
"tables" : {
"Posts": {
...data
},
"NumPosts":{
...data
}
}
}
I went the route of "Use a lambda resolver and move all logic there instead of the standard resolver." for my app.
You have a lot of control this way, solid logging, metric, and can pretty much do whatever you want, but you have to then pay for lambda.
Your option of watching a stream might make more sense if the other changes could happen after the fact, and having the data in an in-between state didn't matter.
Hard to assess what the best option is without total context.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With