apollo-feature-requests
apollo-feature-requests copied to clipboard
useLazySubscription hook
Similar to useLazyQuery, I'd like to request for a hook that would provide the ability to defer the invocation of a subscription query: useLazySubscription
. The problem to be addressed here is therefore similar to useLazyQuery's. This would be useful in cases where the query is dependent on variables that are unavailable during initialization.
This would add another method in the Apollo hooks API, no change in current behavior expected.
The functionality of the proposed useLazySubscription hook can probably be achieved by combining some workarounds - some of which I can think of are
- resubscription once variables are resolved
- nesting useSubscription in a child component
These approaches will likely have a negative impact on maintainability. Having a dedicated hook like useLazySubscription seems to be a more elegant and maintainable solution.
I also need that. Here is some doc that can help https://community.apollographql.com/t/is-it-possible-to-cancel-graphql-subscriptions/116
Any update on this? What is the recommended way to fire subscriptions on lets say an onclick event, if we dont have a lazy hook available?
Hey @kaiz123 👋
I'm curious if the subscribeToMore
function returned by useQuery
would work for you here? You can see an example of this in the "Subscribing to updates for a query" section in our docs. That function can be executed in an onClick
handler to kick off a subscription.