VirtusLab-Open-Source / strapi-plugin-comments

A plugin for Strapi Headless CMS that provides end to end comments feature with their moderation panel, bad words filtering, abuse reporting and more.
MIT License
405 stars 63 forks source link

[CU-23e47df] Pagination not working on V4 #111

Closed be90728 closed 2 years ago

be90728 commented 2 years ago

I can't use pagination in query {{host}}/api/comments/api::post.post:1/flat?pagination[pageSize]=1 Always got error Undefined attribute level operator pageSize

I looked at the source code and I found the function findAllFlat in server/controllers/client.js and found that sort and pagination definitely does not exist in ctx.

So i changed const { params = {}, query, sort, pagination } = ctx; to const { params = {}, query } = ctx;

And add const { sort, pagination } = query; before call service findAllFlat

Also i go to server/services/common.js add code in function findAllFlat like this

delete query.pagination;
if (pagination && isObject(pagination)) {
    const { page = 1, pageSize = 10 } = pagination;
    queryExtension = {
        ...queryExtension,
        offset: (page - 1) * pageSize,
        limit: pageSize,
    };
}

Plugin version: 2.0.4 & 2.0.3

cyp3rius commented 2 years ago

Good catch @be90728, but seems we must do it a bit different to cover also GQL. Anyway will follow your snippets :)

cyp3rius commented 2 years ago

Fix has been released as part of v2.0.5

Tragio commented 2 years ago

@cyp3rius seems that the pagination[withCount] is not workig. The idea is to load x comments and then if there are more, show a "load more" button, however, pagination[withCount]=true does not have any effect on the response and I can't see how much comments are there 🤔

cyp3rius commented 2 years ago

Let me check that @Tragio

Tragio commented 2 years ago

@cyp3rius here it goes my request 😃 /comments/content:1/flat?pagination[page]=1&pagination[pageSize]=10&pagination[withCount]=true&sort[0]=createdAt:desc

Screenshot 2022-02-17 at 17 25 21

cyp3rius commented 2 years ago

@Tragio released as part of v2.0.6. Release notes: https://github.com/VirtusLab-Open-Source/strapi-plugin-comments/releases/tag/v2.0.6

Kaherdin commented 2 years ago

I'm using version 2.0.8 and I still can get the pagination to work. This is my graphQL request :

query commentsApi($apiAndId: String!) { findAllFlat(pagination: {page: 1, pageSize: 10}, relation: $apiAndId, filters: { blocked: { ne: true } }) { data { blocked id author { name } content } meta { pagination { page pageSize pageCount total } } } }

And my response :

{
  "data": {
    "findAllFlat": {
      "data": [

        {
          "blocked": false,
          "id": 15,
          "author": {
            "name": "Test"
          },
          "content": "test"
        }
      ],
      "meta": {
        "pagination": {
          "page": 1,
          "pageSize": 10,
          "pageCount": null,
          "total": null
        }
      }
    }
  }
}

The total is always null

cyp3rius commented 2 years ago

Hello @Kaherdin , right I've checked the pagination type for GQL in Strapi and it doesn't allow to use withCount as for REST API. Let me drop this fix in next version.

cyp3rius commented 2 years ago

@Kaherdin should be fixed in version v2.0.9

Kaherdin commented 2 years ago

Wow great thanks ! You are so reactive, it works now, but I will need to do some more test, because with it's get me the total of entry, including the blocked comments. So it's not accurate for pagination.

cyp3rius commented 2 years ago

You must filter out blocked comments if you want to. By default only removed are filtered out on the client endpoints.

Most of consumers handles comments which are blocked by special visualization.

For total, the same conditions are used.

Nisthar commented 2 years ago

@cyp3rius doesn't pagination works if you are not using flat? I am getting Undefined attribute level operator page

Tragio commented 2 years ago

@cyp3rius doesn't pagination works if you are not using flat? I am getting Undefined attribute level operator page

Yes, you need to use flat. Not sure if that will change.

Nisthar commented 2 years ago

@cyp3rius doesn't pagination works if you are not using flat? I am getting Undefined attribute level operator page

Yes, you need to use flat. Not sure if that will change.

I see, its just hard to group child comments together when using it.

cyp3rius commented 2 years ago

Its indeed tricky case and I'm analyzing couple possibilities here. Happy to work with you folks to get it done as community expect :)