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

Custom ApolloLink using next.subscribe is making duplicated requests #11871

Closed
jca41 opened this issue May 29, 2024 · 9 comments
Closed

Custom ApolloLink using next.subscribe is making duplicated requests #11871

jca41 opened this issue May 29, 2024 · 9 comments

Comments

@jca41
Copy link

jca41 commented May 29, 2024

Issue Description

Given the example custom link

export class MyLink extends ApolloLink {
  constructor() {
    super((operation, forward) => {
      const next: Observable<FetchResult> = forward(operation);

      next.subscribe({
        error: (e) => {
          console.log(e);
        },
        next: (result) => {
          console.log(result);
        },
      });

      return next;
    });
  }
}

By using next.subscribe i'm seeing duplicated queries/mutations.

This happened after bumping from 3.7.14 to 3.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 version

3.10.4

@jca41
Copy link
Author

jca41 commented May 29, 2024

Updated description with repro link: https://github.com/jca41/link-subscribe-duplicated-requests

@jca41
Copy link
Author

jca41 commented May 29, 2024

Also would suggest the team to have a bug report template that uses an external API via HTTP.

@jerelmiller
Copy link
Member

jerelmiller commented May 29, 2024

Hey @jca41 👋

This is actually expected behavior due to how observables work. Anytime you subscribe to it, it will execute that observable's callback function. If you look at HttpLink, you'll see that callback function is where the fetch happens. In the example you've given, you're calling subscribe in the link and returning the same observable back to ApolloClient which also subscribes itself to that observable, hence why you end up with two network requests.

There are a couple different ways you can fix this:

1: Use .map instead of .subscribe which lets you observe the emitted result but returns a new observable back to Apollo Client core. The downside here is that you'll only be able to observe the next values and not the error values. If you need access to errors, try the next technique.

export class MyLink extends ApolloLink {
  constructor() {
    super((operation, forward) => {
      return forward(operation).map((result) => {
        console.log(result);
        return result;
      });
    });
  }
}
  1. Return a new observable that emits results from the forwarded observable. This ensures the observable that Apollo Client subscribes to isn't the same one that creates the network request. This is the technique that several of our built-in links do in order to add additional behavior.
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!

@jca41
Copy link
Author

jca41 commented May 29, 2024

Ah! I guess something got fixed in later versions. Many thanks for the explanation, I will try your suggestions 😊

@jerelmiller
Copy link
Member

Definitely interesting that it deduped before since it probably shouldn't have 😆. Hope the suggestions work for you 🤞

@github-actions github-actions bot removed the 🏓 awaiting-contributor-response requires input from a contributor label May 30, 2024
@jca41
Copy link
Author

jca41 commented May 30, 2024

Thanks @jerelmiller second option seems to be working great! 😊

@jca41 jca41 closed this as not planned Won't fix, can't repro, duplicate, stale May 30, 2024
Copy link
Contributor

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.

@jerelmiller
Copy link
Member

Glad to hear it!

Copy link
Contributor

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.
For general questions, we recommend using StackOverflow or our discord server.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 30, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants