Skip to content

Subscription implementation questions #378

@gfmartinez

Description

@gfmartinez

Some questions about implementing subscriptions.

The docs show the suggested/example setup:

import Route from '@ember/routing/route';
import { queryManager } from 'ember-apollo-client';
import query from 'app/gql/subscriptions/new-human';
import { addListener, removeListener } from '@ember/object/events';

const handleEvent = event => alert(`${event.name} was just born!`);

export default Route.extend({
  apollo: queryManager(),

  model() {
    return this.get('apollo').subscribe({ query }, 'human');
  },

  setupController(controller, model) {
    addListener(model, 'event', handleEvent);
  },

  resetController(controller, isExiting, transition) {
    if (isExiting) {
      removeListener(controller.model, 'event', handleEvent);
    }
  }
});

I ran into several issues with this setup.
Most problematic is that the apollo.subscribe() function returns an object of type EmberApolloSubscription with a structure like

EmberApolloSubscription {
  lastEvent: { human.attrs },
  _apolloClientSubscription: Subscription info
}

In the cases of apollo.query and apollo.watchQuery we get a POJO returned, which fits well with the use of model in the route and controller. However, here we are getting the EmberApolloSubscription which requires the listener setup to extract the real model that we're after. We therefore can't rely on using controller.model in the route templates or for passing down to child components.

Instead of the setup suggested, and using Octane, we had to use something like:

import Route from '@ember/routing/route';
import { queryManager } from 'ember-apollo-client';
import query from 'app/gql/subscriptions/new-human';
import { addListener, removeListener } from '@ember/object/events';

export default class SubscribedRoute extends Route {
  @queryManager apollo;

  model(params) {
    const variables = { id: params.id };
    return this.apollo.subscribe({
      query,
      variables
    }, 'humanWasModified.human');
  }

  setupController(_controller, model) {
    super.setupController(...arguments);
    addListener(model, 'event', this, 'updateSubscribedModel');
  }

  resetController(controller, isExiting) {
    if (isExiting) removeListener(controller.model, 'event', this, 'updateSubscribedModel');
  }

  updateSubscribedModel(humanModel) {
    const controller = this.controllerFor(this.routeName);
    controller.subscribedModel = humanModel;
  }
}

And the controller then has @tracked subscribedModel that will propagate changes when changed.

This works fine, but a bit counter-intuitive and not well documented as to how to deal with this.

Am I missing something? Or is there a better way to do this?

I had thought about hijacking the model by using the event listener to update the controller.model instead of a differently named and tracked property, but that seemed confusing and anti-pattern and made debugging difficult.

Our setup uses ActionCable to establish the web socket link, but that is aside from this issue. The most notable thing there was that we had to make sure that we returned the desired model information on first subscription, but that's no big deal to accomplish.

Thanks in advance for any thoughts and help here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions