如何在 graphql 中获取最后 3 个元素
How in graphql get last 3 element
如何在 graphql.js 中从 mysql db table 中获取最后 3 个 created_at 元素?
我用 sequelize.js
像这样:
query: '{
elements(last:3){
id
}
}'
这是我的文件db.js
const Conn = new Sequelize(/*connection config*/);
Conn.define('elements', {
id: {
type: Sequelize.STRING(36),
primaryKey: true,
allowNull: false
}
});
export default Conn;
这是我的文件schema.js
const Query = new GraphQLObjectType({
name: 'Query',
description: 'Root query object',
fields() {
return {
elements: {
type: new GraphQLList(Element),
args: {
id: {
type: GraphQLString
}
},
resolve (root, args) {
return Db.models.elements.findAll({ where: args });
}
}
}
}
});
一定是这样的;
...
return Db.models.elements.findAll({
limit: 3,
where: args,
order: [['created_at', 'DESC']]
});
http://docs.sequelizejs.com/manual/tutorial/querying.html#pagination-limiting
http://docs.sequelizejs.com/manual/tutorial/querying.html#ordering
如何在 graphql.js 中从 mysql db table 中获取最后 3 个 created_at 元素? 我用 sequelize.js
像这样:
query: '{
elements(last:3){
id
}
}'
这是我的文件db.js
const Conn = new Sequelize(/*connection config*/);
Conn.define('elements', {
id: {
type: Sequelize.STRING(36),
primaryKey: true,
allowNull: false
}
});
export default Conn;
这是我的文件schema.js
const Query = new GraphQLObjectType({
name: 'Query',
description: 'Root query object',
fields() {
return {
elements: {
type: new GraphQLList(Element),
args: {
id: {
type: GraphQLString
}
},
resolve (root, args) {
return Db.models.elements.findAll({ where: args });
}
}
}
}
});
一定是这样的;
...
return Db.models.elements.findAll({
limit: 3,
where: args,
order: [['created_at', 'DESC']]
});
http://docs.sequelizejs.com/manual/tutorial/querying.html#pagination-limiting http://docs.sequelizejs.com/manual/tutorial/querying.html#ordering