我使用Apollo / graphql / sequelize来获取以下关系:
我有多个标记。每个只有一种风格。标记可以使用相同的样式:
Sequelize:
var Marker = db.define('markers', {
name: {type: Sequelize.TEXT},
idstyle: {type: Sequelize.INTEGER},
}, {timestamps: false});
const Markers = db.models.markers;
var Style = db.define('styles', {
name: {type: Sequelize.TEXT},
icon: {type: Sequelize.TEXT}
}, {timestamps: false});
Marker.belongsTo(Style, {foreignKey: 'idstyle'});
graphql架构:
type Marker {
id: Int
name: String
style: Style
}
type Style {
id: Int
name: String
icon: String
}
type Query {
marker(limit: Int, offset: Int): [Marker]
style(limit: Int, offset: Int): [Style]
}
解析器:
Query: {
async marker(root, args, context) {
return Markers.findAll({limit: args.limit, offset: args.offset});
},
async style(root, args, context) {
return Styles.findAll({limit: args.limit, offset: args.offset});
}
},
Marker: {
async style(marker) {
return marker.getStyle();
}
}
我意识到在运行以下查询时似乎没有任何智能缓存:
query{
marker{
name
style{
name
}
}
}
似乎相同的样式再次被查询,即使它们已经作为另一个标记的结果返回。您可以看到请求的样式ID重复:
答案 0 :(得分:0)