Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to perform batch Create/Add mutation for a many-to-many relation in amplify app-sync? #465

Open
socialyadept opened this issue Mar 12, 2023 · 0 comments
Labels

Comments

@socialyadept
Copy link

socialyadept commented Mar 12, 2023

I just finished to test the Many-To-Many Connections example and It worked for me. However, I still can't figure it out how to use only one mutation (and if it's even possible) to achieve the same result as the one given in the documentation. For example:

mutation CreateLinks {
    p1u1: createPostEditor(input: { id: "P1U1", postID: "P1", editorID: "U1" }) {
        id
    }   
    p1u2: createPostEditor(input: { id: "P1U2", postID: "P1", editorID: "U2" }) {
        id
    }
}

So far I was able to handle this with promise, but I'm not sure if this is the best approach (because it involves to execute as much mutations as there are users):

        const users = [{id: "U1", username: "user1"}, {id: "U2", username: "user2"}];
        const post = { id: "P1", title: "Post 1" };
        /*
        After creating two users and a post using the appropriate mutations
        Using the CreatePost join below to make user1 and user2 editor on Post 1
        */
        function graphqlCreatePostEditor(editorID) {
            return new Promise((resolve, reject) => {
              resolve(
                API.graphql(graphqlOperation(createPostEditor, {
                  input: {
                      postID: post.id,
                      editorID
                    }
                }))
              )
            })
          }
  
        let promises = users.map(user=> {
            return graphqlCreatePostEditor(user.id)
            .then(e => {
                console.log(e)
            return e;
            })
        });

        Promise.all(promises)
            .then(results => {
                console.log(results)
            })
            .catch(e => {
                console.error(e);
            })

Originally posted by @devnantes44 in aws-amplify/amplify-cli#91 (comment)

Anyone having a solution to this? I just want to create a mutation that can first create the post and add all the editors let's say they come from an array directly into the join table of PostEditor table. Any clue?

@ykethan ykethan transferred this issue from aws-amplify/amplify-cli Mar 13, 2023
@nadetastic nadetastic assigned nadetastic and unassigned nadetastic Mar 13, 2023
@iartemiev iartemiev added the question Further information is requested label Mar 23, 2023
@Jay2113 Jay2113 added feature-request New feature or request and removed question Further information is requested labels Aug 14, 2023
@stocaaro stocaaro transferred this issue from aws-amplify/amplify-js Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants