Linked Questions

Popular Questions

AWSAppSync GraphQL Mutation executed twice

Asked by At

So I don't know if this actually describes my problem, but is it the closest I could get.

I'm using the AWSAppsyncClient to do some GraphQL mutations. Due to the nature of the model, and to a greater degree, my inexperience, I need to create a main record, and then two records that depend on the first one, before I can link these in the database.

Currently, I'm doing the first mutation, and it returns the ID of the created record. The intermediary records are then created in the promises that query returns. It basically looks like:

AppsyncClient.mutate(mutation,parameters)//first api call
.then( function (res){ 
 // create the new object from the response of the mutation

return AppsyncClient.mutate(mutation,parameters)})//second api call
.then( function (res){ // second API Call
 // create the new object from the response of the mutation

return AppsyncClient.mutate(mutation,parameters)})

The problem is that the promise reruns the original request against my API. For example, the first record gets created, and the id returned, but then in the second API call the first request is somehow executed again, alongside the new one, resulting in two of the original records being created. This continues as long as the chain continues, always executing the previous request again, creating two records for every new object. (This only happens when another query is done in a promise.)

I don't know if this is clear enough, I'm happy to provide more details if needed, but can someone tell me what I'm doing wrong?

Related Questions