When people subscribe to something, they won’t get data immediately. They have to wait for next change to happen. I’d like to send some initial data when people subscibe.
I’m reading this doc, Subscriptions in Apollo Server - Apollo Server - Apollo GraphQL Docs, but find no way to do this.
For example, there is an online-people-count entry. when people subscribe, I’d like them to know how many people are online immediately, instead of wait for next online-people-count change.
Hello! To get initial data immediately and then receive updates as they occur via subscription, I recommend starting with a query operation and then using the
subscribeToMore function to subscribe to updates to that data.
This pattern is covered here in the Apollo Client docs: Subscribing to updates for a query
Please take a look and feel free to follow up if you hit any bumps!
Thank you. This is helpful in other cases, but in my case there is a problem.
I will increase the online counter by one and fire a change when
onConnect is called for an incoming websocket of apollo server. If I use fetch first, since there is still no websocket connection made by the caller, the online counter will be 1 less than real number. Then when the caller calls
subscribeToMore the counter will be increased by one, but the caller won’t get the new value until the next person make any subscription to the server.