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

It is not possible to wait for subscription to subscribe #8886

Closed
aleksandrlat opened this issue Oct 3, 2021 · 4 comments
Closed

It is not possible to wait for subscription to subscribe #8886

aleksandrlat opened this issue Oct 3, 2021 · 4 comments

Comments

@aleksandrlat
Copy link

Intended outcome:
I want to implement response for mutation to come through websocket.
I.e.

  • subscribe to subscription
  • call mutation to trigger some long running process on server
  • receive result of long running process in subscription message

But I can call mutation only after I can confirm I subscribed to subscription

Actual outcome:
loading is true when I subscribed to subscription

const { loading, data } = useSubscription(subscription)
// loading is true when I subscribed to it

How to reproduce the issue:
Just subscribe to subscription

Versions
version 3

@rossimelthomas
Copy link

any updates?

@phryneas
Copy link
Member

phryneas commented Aug 1, 2024

I believe you can use the onData callback of useSubscription for this.

function MyComponent() {
  const calledRef = useRef(false);
  const { loading, data } = useSubscription(subscription, {
    onData({ data }) {
      if (!calledRef.current) {
        calledRef.current = true;

		console.log(data)
        // execute your mutation here
      }
    },
  });
}

I believe beyond that, we can't really do a lot here - React doesn't wait for something to happen before rendering, it just synchronously renders your component.

As a result I'm going to close this issue - if you have any further usage questions, please also consider joining our Discord - we try to keep the issue tracker for bug reports and usage questions more in the Discord.

@phryneas phryneas closed this as completed Aug 1, 2024
Copy link
Contributor

github-actions bot commented Aug 1, 2024

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.

Copy link
Contributor

github-actions bot commented Sep 1, 2024

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 Sep 1, 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

4 participants