Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access parent data from within child instanceMethods #5881

Closed
HriBB opened this issue May 13, 2016 · 2 comments
Closed

Access parent data from within child instanceMethods #5881

HriBB opened this issue May 13, 2016 · 2 comments

Comments

@HriBB
Copy link

HriBB commented May 13, 2016

Already asked this question on Apollostack graphql-tools issue tracker, but this is probably a sequelize thing ...

Anyway, I have a parent->children relation on the same table, and I am trying to access parent data from child instanceMethods

This is my sequelize model Location

module.exports = function(sequelize, DataTypes) {
  return sequelize.define('Location', {
    parent_id: {
      type: DataTypes.INTEGER(11),
      allowNull: true,
    },
    name: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    slug: {
      type: DataTypes.STRING,
      allowNull: false,
    }
  },{
    tableName: 'location',
    freezeTableName: true,
    instanceMethods: {
      getUrl: function() {
        // here I need to check if this instance is a child
        // and return a different url for child
        return '';
      }
    },
    classMethods: {
      associate: function(m) {
        m.Location.belongsTo(m.Location, {
          foreignKey: 'parent_id',
          as: 'Parent'
        });
        m.Location.hasMany(m.Location, {
          foreignKey: 'parent_id',
          as: 'Locations'
        });
      }
    }
  });
};

And this are my resolve functions, which is part of the Apollostack workflow.

const resolveFunctions = {
  RootQuery: {
    location(root, { slug }, context){
      return Location.find({ where: { slug }, include:[{ model: Location, as: 'Locations' }] });
    }
  },
  Location: {
    parent(location){
      return location.getParent();
    },
    locations(location){
      return location.getLocations();
    },
    url(location){
      // or here ... 
      // check if this location is child
      // and return a different url
      return location.getUrl();
    }
  }
}

What would be the best way to do this?

This is the solution I have come up with. I manually inject parent data into child, but it feels like there's a better way ...

const resolveFunctions = {
  RootQuery: {
    // ...
  },
  Location: {
    locations(location){
      if (!location.Locations) {
        return [];
      }
      // I can manually "inject" parent into each location
      // this way I can access this.parent from within getUrl() inside instanceMethods
      return location.Locations.map(l => {
        l.parent = location.dataValues;
        return l;
      });
    }
  }
}

Dialect: mysql v2.10.2
Database version: 5.7.10
Sequelize version: 3.22.0

@janmeier
Copy link
Member

janmeier commented May 13, 2016

There is no way to access the parent without explictly loading it (this.getParent().then...). Its a tradeof of performance (injecting already loaded data) vs. separation (loading data where its needed) I guess.

If you're not too deeply invested in graphql-tools, have a look at https://github.com/mickhansen/graphql-sequelize/ ;)

Closing this issue, since it is neither a bug report nor a feature request.

For general sequelize questions, please use Slack, StackOverflow or Google groups.

@HriBB
Copy link
Author

HriBB commented May 13, 2016

Thanks for the info and sorry for the spam ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants