Skip to content

Suggested Use For WatchQuery on Data-Loading Components #292

@topbloc-beiswenger

Description

@topbloc-beiswenger

I was unable to find anything in the docs, tutorials, or example projects about how to exactly use watch queries in data-loading components. From the docs, I know that I can use the ComponentQueryManager which will track active watch queries and unsubscribe when the component is destroyed. I also know that watchQuery will return an Ember.Object and can be used in computed properties. What I don't know is how an actual implementation looks. Based on the above information, I would assume that the following works but it does not.

import Component from '@ember/component'
import { computed } from '@ember/object'
import { inject as service } from '@ember/service'
import { ComponentQueryManager } from 'ember-apollo-client'
import query from 'tbui/gql/queries/pets'

export default class SideBar extends Component.extend(ComponentQueryManager) {

  async didInsertElement() {
    let variables = { ownerId: '1' }
    let result = await this.apollo.watchQuery(
      {
        query,
        variables,
      },
      'pets'
    )
    this.set('activeQuery', result)
  }

  @computed('activeQuery')
  get numberOfPets() {
    console.log('query updated')
    return this.activeQuery ? this.activeQuery.length : null
  }
}

The computed property never gets updated even though it is utilized in the template.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions