The Wayback Machine - https://web.archive.org/web/20200604221100/https://github.com/apollographql/react-apollo/issues/3577
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

No documentation for resubscribe, unsubscribe in useSubscription hook #3577

Open
wmwart opened this issue Oct 9, 2019 · 3 comments
Open

No documentation for resubscribe, unsubscribe in useSubscription hook #3577

wmwart opened this issue Oct 9, 2019 · 3 comments

Comments

@wmwart
Copy link

@wmwart wmwart commented Oct 9, 2019

Hello!
I wrote a Notification service for my application, and use a subscription to a new notification for the user, according to the selected topics.

After changing the topics for the user, it is logical to resubscribe to notifications with new parameters, but nowhere in the documentation for useSubscription hook there is no mention or example for such a common scenario.

I ask you to help with the documentation if I missed it or with an example of how I can implement my plan.

import gql from 'graphql-tag'
import { useSubscription } from '@apollo/react-hooks';

export const SUBSCRIBE_NOTIFICATIONS = gql`
    subscription notification( $filter: NotificationFilter, $mutations: [String]) {
        notification(filter: $filter, mutations: $mutations) {
            mutation
            node {            
                id
                timestamp
                ttl
                ttlAt
                app
                title
                body
            }
        }
    }
`
...
const onSubscriptionData = ({client, subscriptionData: {data: { notification: { mutation, node }}}}) => { ... };

const notificationSubscription = useSubscription(SUBSCRIBE_NOTIFICATIONS, {
            // shouldResubscribe: ?????,
            client: apolloNotificationsClient,
            onSubscriptionData: onSubscriptionData,
        }
    );
...

Version
@apollo/react-hooks: 3.1.1

@THEjoezack
Copy link

@THEjoezack THEjoezack commented Nov 9, 2019

I found the code for useSubscription, but I don't see a way to access the underlying subscription object that is used to unsubscribe:

https://github.com/trojanowski/react-apollo-hooks/blob/master/src/useSubscription.ts

@LydiaF
Copy link

@LydiaF LydiaF commented Feb 6, 2020

Would also very much appreciate help with this issue :)

@DominicTobias-b1
Copy link

@DominicTobias-b1 DominicTobias-b1 commented Feb 7, 2020

If you need more control you could use it programmatically e.g:

export const useSomeSubscription = (marketId: string): OrderbookData => {
  const [data, setData] = useState(<OrderbookData>{ bids: [], asks: [] })
  const client = useApolloClient()

  useEffect(() => {
    const observer = client.subscribe<OrderBook, OrderBookVariables>({
      query: ORDERBOOK_SUBSCRIPTION,
      variables: {
        marketId,
      },
    })

    const subscription = observer.subscribe(({ data }) => {
      console.log('SUBSCRIBE received', data)
      setData(data);
    })

    return () => subscription.unsubscribe()
  }, [marketId])

  return data
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
4 participants
You can’t perform that action at this time.