mst-gql
mst-gql copied to clipboard
How do I update data from server?
So far I've been able to query (once) and mutate data using mst-gql. In order to keep data fresh, in another project using apollo and a polling approach, I can do something like
useQuery<QueryResponseType>(QUERY, {
variables,
pollInterval: 1000,
onCompleted(data) {
state.updateData(data);
},
...
}
which will automatically keep the state up to date with the server.
Is the equivalent of this in MST-GQL to setup a subscription using store.subscribeDataModel(...)
? Or is there another way to setup polling so these changes are reflected in near real-time?
Technically there's nothing stopping you from hand-writing a polling setup (e.g. use setInterval to repeatedly call the same query) but personally I use subscriptions as I believe polling to be wasteful. Either way, there's no need for a state.updateData
because of Mobx's reactivity. Make sure your React components are setup properly to work with Mobx and everything should update automatically.