Stop Multiple returns from Server after query with Apollo and Angular

Issue

Bellow is an example on code I am using to submit data to a server and the return I receive and save

this.apollo.mutate( { mutation: XXXXXXXX, variables: { instance_string: X, 
accesstoken: X } })
.subscribe({
   next: (data: any ) => {
     console.log("data returned from Server", data); 
     // This data can be sent from the Server in a loop on occassion
     this.SaveData(data);
   },
   error: (err) => {
     this.presentToastFail(err);
   }
 });

On occasion the Server returns the data in a loop. I have no control of the Server and it appears this will be a reoccurring bug for quite a while. Is there a way that I can can insure the next: only runs once and ignores the Loop of data returns the Server can send.

Solution

It looks like you have to use two pipeable operators like take(), filter():

.pipe(
  filter(resp => resp !== undefined && resp !== null),
  take(1) // <--- It will only take one successful valid response
)
.subscribe(...)

Answered By – Jai

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published