aws-amplify / amplify-category-api

The AWS Amplify CLI is a toolchain for simplifying serverless web and mobile development. This plugin provides functionality for the API category, allowing for the creation and management of GraphQL and REST based backends for your amplify project.
https://docs.amplify.aws/
Apache License 2.0
88 stars 76 forks source link

How to create multiple batch mutations #2834

Closed pjsandwich closed 1 week ago

pjsandwich commented 1 week ago

Amplify CLI Version

12.12.6

Question

Which of these are the correct way to create more than one batch mutation with a custom cdk resolver?

type Mutation {
  batchCreateTodo(todos: [BatchCreateTodo]): [Todo]
}

type OtherMutation {
...
}

Or

type Mutation {
  batchCreateTodo(todos: [BatchCreateTodo]): [Todo]
  someOtherBatch(things: [BatchCreateThings]): [Things]
}

I see that the typeName field should be Query | Mutation | Subscription:

const resolver = new appsync.CfnResolver(this, 'batchCreateToDo', {
      apiId: apiIdRef,
      fieldName: 'batchCreateToDo',
      typeName: 'Mutation', // Mutation type
      requestMappingTemplate: requestVTL,
      responseMappingTemplate: responseVTL,
      dataSourceName: 'ToDoTable', // DataSource name
    });

But how should an additional batch mutation be added to the schema and does the typeName or the resolver code need to be adjusted in a certain way on the existing toDo resolver or the new resolver?

AnilMaktala commented 1 week ago

Hey @pjsandwich, Thank you for raising this. Yes, you can create multiple batch mutations using the same approach as batchCreateToDo, with the TypeName always being Mutation in this case. Below is a sample code snippet for BatchCreateThings.

type Mutation {
  batchCreateTodo(todos: [BatchCreateTodo]): [Todo]
  someOtherBatch(things: [BatchCreateThings]): [Things]
}

const thingsresolver = new appsync.CfnResolver(this, 'someOtherBatch', {
      apiId: apiIdRef,
      fieldName: 'someOtherBatch',
      typeName: 'Mutation', // Mutation type
      requestMappingTemplate: requestVTL,
      responseMappingTemplate: responseVTL,
      dataSourceName: 'ThingsTable', // DataSource name
    });
pjsandwich commented 1 week ago

Thank you @AnilMaktala , I will try this out and get back to you here

pjsandwich commented 1 week ago

This works! Thanks @AnilMaktala

github-actions[bot] commented 1 week ago

This issue is now closed. Comments on closed issues are hard for our team to see. If you need more assistance, please open a new issue that references this one.