Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How in graphql get last 3 element

Tags:

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 });
                }
            }
        }
    }
});
like image 533
pmnazar Avatar asked Jun 08 '17 12:06

pmnazar


1 Answers

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

like image 124
Barış Serkan AKIN Avatar answered Oct 04 '22 16:10

Barış Serkan AKIN