简体   繁体   中英

How in graphql get last 3 element

How in graphql.js get last 3 created_at element from mysql db table? I use sequelize.js

Like this:

query: '{
    elements(last:3){ 
        id
    }
}'

This is my file db.js

const Conn = new Sequelize(/*connection config*/);
Conn.define('elements', {
    id: {
        type: Sequelize.STRING(36),
        primaryKey: true,
        allowNull: false
    }
});
export default Conn;

This is my file 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 });
                }
            }
        }
    }
});

it must be like this;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM