apollo-feature-requests icon indicating copy to clipboard operation
apollo-feature-requests copied to clipboard

useLazySubscription hook

Open bluepropane opened this issue 4 years ago • 1 comments

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.

bluepropane avatar May 24 '20 06:05 bluepropane

I also need that. Here is some doc that can help https://community.apollographql.com/t/is-it-possible-to-cancel-graphql-subscriptions/116

MaxiSantos avatar Oct 01 '21 17:10 MaxiSantos

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?

kaiz123 avatar Oct 27 '23 00:10 kaiz123

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.

jerelmiller avatar Nov 06 '23 20:11 jerelmiller