2

Does AppSync support nested single mutation?

I want to call a single mutation which will insert records into two tables, eg: User and Roles tables in DynamoDB.

Something like this for example:

createUser(
   input: {
      Name: "John"
      Email: "[email protected]"
      LinesRoles: [
        { Name: "Role 1" }
        { Name: "Role 2" }
      ]
   }) {
        Id
        Name
        LinesRoles {
          Id
          Name
        }
      }

Do I need to create two resolvers in AppSync for User and Roles to insert the records in both tables?

1 Answer 1

3

I can think of three ways to achieve this:

  1. Use a BatchPutItem to save records into two tables at once. However, you won’t be able to use any ConditionExpression
  2. Use a pipeline resolver with two AppSync functions where one function makes a PutItem to the Roles table and the other to the User table. However, you need to be ok with potentially inconsistent scenarios where the record has been inserted in one table but not in the other.
  3. Use a Lambda resolver that does the write to 2 tables inside a DynamoDB transaction.
Sign up to request clarification or add additional context in comments.

2 Comments

To use BatchPutItem, how to get new inserted UserId before creating roles in the Roles Table? Do I need 1 or 2 resolvers for this case?
When you use BatchPutItem you have to specify the entire item for both tables at the same time. So in your case you need to know the userId before inserting, maybe use a UUID?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.