aws-amplify / amplify-cli

The AWS Amplify CLI is a toolchain for simplifying serverless web and mobile development.
Apache License 2.0
2.81k stars 821 forks source link

Mutation multiple row data insert to db #1935

Closed meetrij closed 5 years ago

meetrij commented 5 years ago

I have a type as shown below

type MasterCrop @model {
    cropId: ID!
    cropCode: String
    cropName: String
}

Mutation generated against this is like

createMasterCrop(input: CreateMasterCropInput!): MasterCrop
updateMasterCrop(input: UpdateMasterCropInput!): MasterCrop
deleteMasterCrop(input: DeleteMasterCropInput!): MasterCrop

API.service.ts file generated like this

export type CreateMasterCropInput = {
  cropId: string;
  cropCode?: string | null;
  cropName?: string | null;
};

suppose I want to add multiple data like

1, RC, Rice
2, WT, Wheat
3, CN, Corn

in a single API call, how could I do? As now only provision to add single row item at a time.

pigmanbear commented 5 years ago

See batch resolvers in the Appsync documentation:

https://docs.aws.amazon.com/appsync/latest/devguide/tutorial-dynamodb-batch.html

But, depending on your use case, just remember dynamo has a batch limit of 25 (splitEvery by ramda in a composition can come in handy there)

BatchWriteItem A single BatchWriteItem operation can contain up to 25 PutItem or DeleteItem requests. The total size of all the items written cannot exceed 16 MB.

Or, when performing the mutation, you can do something like :

const CreateMasterCrop = `mutation CreateMasterCrop($input: CreateMasterCropInput! ){
  createMasterCrop(input: $input){
    ${MasterCropInput}
  }
}`

const crops = [
  { input: { cropId: 1, cropCode: 'RC', cropName: 'Rice' } },
  ...otherCrops,
]
const mutateManyItems = mutation => async items =>
  Promise.all(
    items.map(input => API.graphql(graphqlOperation(mutation, input)))
  )

mutateManyItems(CreateMasterCrop)(crops).then(...).catch(...)
kaustavghosh06 commented 5 years ago

@meetrij I agree with @pigmanbear's explainaton but you should be aware of AppSync throttling levels if you're using a Proime.all() from your client - it's basically 1,000 queries per second. You can find more information out here - https://docs.aws.amazon.com/general/latest/gr/aws_service_limits.html#limits_appsync

github-actions[bot] commented 3 years ago

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.