Trying to write a wrapper around useQuery for use with graphql subscriptions.
Hi all, sorry for the noob question, I'm still relatively new to react and js.
I'm trying to write a 'useSubscription' hook which does the following against a graphql backend:
- returns the result of the graphql query using useQuery, this returns quickly and populates the cache with initial data
- inits a graphql subscription using the same graphql operation as done in the initial query, and when new data comes in it updates the react-query cache
I have this working but the problem is deduplicating the subscriptions if the hook is mounted by multiple components. Somehow react-query is able to only run the fetch function once if mounted multiple times, but I need a way to hook into the same mechanism and perform by 'client.subscribe' side affect only once. The following code will perform it multiple times for each mounted component (assuming they're all mounted at the same time). As you can see I've tried to get access to the number of mounted components using 'getObserversCount' but it doesn't seem to work.
https://gist.github.com/armincerf/df2a40194bcfe487510f56c66f99a693
Any ideas?
0 Replies