Search code examples
apigraphqlmutationaws-amplify

Need advices on multiple query Amplify QraphQL


I am developping an application using AWS Amplify. I am using the Graphql API to manage my Dynamodb tables.

I need some advices to know the best way to mutate multiple Items in a table. I read that Qraphql and dynamodb are able to mutate 1000 items per second, but actualy this performance is not working on my way :(.

To mutate multiple items i have an array with all my queries and input parameters, and i am using Promise.all function like :

await Promise.all(
  options.data.map(  item => 
     API.graphql(
       graphqlOperation(
          item.query, 
          { input: item.input }
       )
     )
   )
);

Is it the good way to do that ? it's working but with 1000 items it tooks 4/5 seconds. Could you advice me how to improve the performance of multiple item mutation using Amplify graphQl API ? Thank you very much


Solution

  • Try sending all your queries or mutations with 1 request instead of using API.graphql multiple times.

    For example this how I delete the post's comments when I delete the post.

    const commentMutations: any = comments.map(
      (comment: Comment, i: number) => {
        return `mutation${i}: deleteComment(input: {id: "${comment.id}"}) { id }`;
      }
    );
    
    await API.graphql(
      graphqlOperation(`
      mutation deletePostComments {
        ${commentMutations}
      }
    `)
    );