You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
The text was updated successfully, but these errors were encountered:
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.
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
And this are my resolve functions, which is part of the Apollostack workflow.
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 ...
Dialect: mysql v2.10.2
Database version: 5.7.10
Sequelize version: 3.22.0
The text was updated successfully, but these errors were encountered: