【问题标题】:How in graphql get last 3 element在graphql中如何获取最后3个元素
【发布时间】:2017-11-10 04:46:36
【问题描述】:

graphql.js 如何从 mysql db 表中获取最后 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 });
                }
            }
        }
    }
});

【问题讨论】:

    标签: express sequelize.js graphql graphql-js


    【解决方案1】:

    一定是这样的;

    ...
    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

    【讨论】:

    • 感谢您的回答。但是我怎样才能改变我的 scheme.js?像这样? lastThreeElements: { type: new GraphQLList(Element), args: { id: { type: GraphQLString } }, resolve (root, args) { return Db.models.elements.findAll({ limit: 3, where: args, order: [['created_at', 'DESC']] }); } }
    猜你喜欢
    • 2011-07-25
    • 2019-07-14
    • 2013-04-22
    • 2018-07-27
    • 2012-11-20
    • 1970-01-01
    • 2010-10-09
    • 2020-04-06
    • 1970-01-01
    相关资源
    最近更新 更多