Search code examples
node.jsstrapi

Implementing pagination on article in strapi


async function find(ctx) {
    return await strapi.services.article.find(ctx.query)
}

This is my controller

resolver: async (obj, options, ctx) => {
    const result = await strapi.query('active').model.find({ article_id: obj.id}); 
    return result.length > 0;
}

This is my resolver

I have this on my strapi backend and I am trying to figure out how to do pagination. Because there are so many ways to go about it and it's not documented very well, I am not sure what we can do. Is this using Mongoose and also, how do you send the data from the controller to the resolver exactly?

resolver: async (obj, options, ctx) => {
    const result = await strapi.query('active').model.find({ article_id: obj.id}).limit(obj.limit).offset(obj.offset); 
    return result.length > 0;
}

Is it possible to do this since I am assuming it is mongoose that's being used? If not, how can we change the resolver to paginate the articles?


Solution

  • for strapi v3, you can use something like this:

    const result = await strapi.query("model").find({
          id: id,
          _start: page > 0 ? (page - 1) * pageSize : 0,
          _limit: pageSize,
          _sort: "created_at:desc",
    });