Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AppSync mutation that triggers more mutations

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.

like image 781
Z Jones Avatar asked May 25 '18 23:05

Z Jones


People also ask

What is AppSync mutation?

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.

What are AppSync subscriptions?

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.

How do I connect lambda to AppSync?

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.


2 Answers

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
    }
  }
}
like image 181
Karthik Avatar answered Oct 17 '22 17:10

Karthik


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.

like image 40
Michael Economy Avatar answered Oct 17 '22 15:10

Michael Economy