gqless icon indicating copy to clipboard operation
gqless copied to clipboard

Subscriptions & resolved

Open samdenty opened this issue 4 years ago • 0 comments

The current API for subscribing to subscriptions is:

resolved(() => {
  return subscription.notification
}, {
  onSubscription(event) {
    switch (event.type) {
      case "complete"
      case "start":
        {
          event.unsubscribe
           break;
        }
      case "data": {
        event.data;
        break;  
      }
     case "with-errors": {
       event.data;
       event.error;
        break;
      }
    }
  }
})

We need some new helper APIs, following the mobx way should be good

resolved and subscriptions

I think the most intuitive way, is that resolved works with subscriptions the same way as queries (so that you can replace stale data in your app with live).

const notification = await resolved(() => {
  return subscription.notification
})

autorun

For listening to a stream of data

const unlisten = autorun(() => {
   // This console logs every time a new notification comes in
   // or when query.something changes in the cache
   console.log(subscription.notification)

   console.log(query.something)
}, (error) => {
  // when an error comes in
  console.log(error)
});

// stops listening for changes
unlisten()

samdenty avatar Apr 12 '21 19:04 samdenty