-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Custom ApolloLink using next.subscribe
is making duplicated requests
#11871
Comments
Updated description with repro link: https://github.com/jca41/link-subscribe-duplicated-requests |
Also would suggest the team to have a bug report template that uses an external API via HTTP. |
Hey @jca41 👋 This is actually expected behavior due to how observables work. Anytime you There are a couple different ways you can fix this: 1: Use export class MyLink extends ApolloLink {
constructor() {
super((operation, forward) => {
return forward(operation).map((result) => {
console.log(result);
return result;
});
});
}
}
export class MyLink extends ApolloLink {
constructor() {
super((operation, forward) => {
return new Observable((observer) => {
const subscription = forward(operation).subscribe({
next: (result) => {
console.log(result);
observer.next(result);
},
error: (e) => {
console.log(e);
observer.error(e);
},
// very important that you include this to ensure you let the observer
// know this operation is completed!
complete: observer.complete.bind(observer)
});
// if the upstream observer stops watching for changes, unsubscribe from the forwarded operation as well
return () => subscription.unsubscribe();
})
});
}
} Try either of these and see if this works better for you! |
Ah! I guess something got fixed in later versions. Many thanks for the explanation, I will try your suggestions 😊 |
Definitely interesting that it deduped before since it probably shouldn't have 😆. Hope the suggestions work for you 🤞 |
Thanks @jerelmiller second option seems to be working great! 😊 |
Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Client usage and allow us to serve you better. |
Glad to hear it! |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Issue Description
Given the example custom link
By using
next.subscribe
i'm seeing duplicated queries/mutations.This happened after bumping from
3.7.14
to3.10.4
.Any help debugging this issue would be appreciated!
Link to Reproduction
https://github.com/jca41/link-subscribe-duplicated-requests
Reproduction Steps
No response
@apollo/client
version3.10.4
The text was updated successfully, but these errors were encountered: