Strapi V4 handle pagination in GraphQL custom resolver

Thank you brother, I finallly found a solution by looking in strapi repository directly.

The “meta” object which contains pagination details is handled by strapi it self using a resolver and it need us to provide resourceUID , start and limit. So we don’t need to use findPage method findMany is enough.

ResponseCollectionMeta: objectType({
      name: RESPONSE_COLLECTION_META_TYPE_NAME,

      definition(t) {
        t.nonNull.field('pagination', {
          type: PAGINATION_TYPE_NAME,

          async resolve(parent) {
            const { args, resourceUID } = parent;
            const { start, limit } = args;
            const safeLimit = Math.max(limit, 1);

            const total = await strapi.entityService.count(resourceUID, args);
            const pageSize = limit === -1 ? total - start : safeLimit;
            const pageCount = limit === -1 ? safeLimit : Math.ceil(total / safeLimit);
            const page = limit === -1 ? safeLimit : Math.floor(start / safeLimit) + 1;

            return { total, page, pageSize, pageCount };
          },
        });
      },

So the key is to return something like this at the end of our custom resolver :

return toEntityResponseCollection(results, {
            args: { start, limit },
            resourceUID: "api::XXXX.XXXX",
          });
1 Like