jeudi 7 février 2019

How to keep data synchronized in ember using ember-apollo-client?

I have an app built using Ember and ember-apollo-client.

// templates/collaborators.hbs

// opens an ember-bootstrap modal
Create collaborator
// submit button in modal triggers "createCollaborator" in controller    


     


// routes/collaborators.js
import Route from '@ember/routing/route';
import { RouteQueryManager } from 'ember-apollo-client';
import query from '../gql/collaborators/queries/listing';

export default Route.extend(RouteQueryManager, {
    model() {
        return this.get('apollo').watchQuery({ query }); 
    }
});

// controllers/collaborator.js
export default Controller.extend({
  apollo: service(),

  actions: {
    createCollaborator() {
      let variables = { 
        firstName: this.firstName, 
        lastName: this.lastName, 
        hireDate: this.hireDate 
      }

      return this.get('apollo').mutate({ mutation, variables }, 'createCollaborator')
        .then(() => {
          this.set('firstName', '');
          this.set('lastName', '');
          this.set('hireDate', '');
      });
    }
  }
});

Currently, after creating a collaborator the data is stale and needs a browser refresh in order to update. I'd like the changes to be visible on the collaborators list right away.

From what I understood, in order to use GraphQL with Ember, I should use either Ember Data with ember-graphql-adapter OR just ember-apollo-client. I went on with apollo because of its better documentation.

I dont think I quite understood how to do that. Should I somehow use the store combined with watchQuery from apollo? Or is it something else?




Aucun commentaire:

Enregistrer un commentaire