Skip to content

Commit ad9e1b9

Browse files
Fix-316: paginated get with children (#344)
* fix: add pagination to get with children endpoint * chore: fix formatting in test file
1 parent ee6c094 commit ad9e1b9

File tree

6 files changed

+201
-110
lines changed

6 files changed

+201
-110
lines changed

README.md

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ In our minimum support we're following [official Node.js releases timelines](htt
110110
> This plugin is designed for **Strapi v5**. To get support for other Strapi versions, please follow the [versions](#-versions) section.
111111
112112
**Plugin dependencies**
113-
- `@strapi/plugin-graphql` - required to run GraphQL handled by this plugin
113+
114+
- `@strapi/plugin-graphql` - required to run GraphQL handled by this plugin
114115

115116
**We recommend always using the latest version of Strapi to start your new projects**.
116117

@@ -134,7 +135,7 @@ On the dedicated page, you will be able to set up all crucial properties which d
134135
To setup amend default plugin configuration we recommend to put following snippet as part of `config/plugins.{js|ts}` or `config/<env>/plugins.{js|ts}` file. If the file does not exist yet, you have to create it manually. If you've got already configurations for other plugins stores by this way, use just the `comments` part within exising `plugins` item.
135136

136137
```ts
137-
module.exports = ({ env }) => ({
138+
module.exports = ({ env }) => ({
138139
//...
139140
comments: {
140141
enabled: true,
@@ -289,6 +290,7 @@ Return a hierarchical tree structure of comments for specified instance of Conte
289290

290291
- [field selection](https://docs.strapi.io/dev-docs/api/rest/populate-select#field-selection)
291292
- [sorting](https://docs.strapi.io/dev-docs/api/rest/sort-pagination#sorting)
293+
- [pagination](https://docs.strapi.io/dev-docs/api/rest/sort-pagination#pagination)
292294

293295
### Get Comments (flat structure)
294296

@@ -666,7 +668,7 @@ query {
666668
"id": "123456",
667669
"name": "Joe Doe"
668670
}
669-
},
671+
}
670672
// ...
671673
]
672674
}
@@ -937,34 +939,32 @@ Lifecycle hooks can be register either in `register()` or `bootstrap()` methods
937939

938940
Listeners can by sync and `async`.
939941

940-
>Be aware that lifecycle hooks registered in `register()` may be fired by plugin's bootstrapping. If you want listen to events triggered after server's startup use `bootstrap()`.
942+
> Be aware that lifecycle hooks registered in `register()` may be fired by plugin's bootstrapping. If you want listen to events triggered after server's startup use `bootstrap()`.
941943
942944
Example:
943945

944946
```ts
945-
const commentsCommonService = strapi
946-
.plugin("comments")
947-
.service("common");
947+
const commentsCommonService = strapi.plugin("comments").service("common");
948948

949-
commentsCommonService.registerLifecycleHook({
950-
callback: async ({ action, result }) => {
951-
const saveResult = await logIntoSystem(action, result);
949+
commentsCommonService.registerLifecycleHook({
950+
callback: async ({ action, result }) => {
951+
const saveResult = await logIntoSystem(action, result);
952952

953-
console.log(saveResult);
954-
},
955-
contentTypeName: "comment",
956-
hookName: "afterCreate",
957-
});
953+
console.log(saveResult);
954+
},
955+
contentTypeName: "comment",
956+
hookName: "afterCreate",
957+
});
958958

959-
commentsCommonService.registerLifecycleHook({
960-
callback: async ({ action, result }) => {
961-
const saveResult = await logIntoSystem(action, result);
959+
commentsCommonService.registerLifecycleHook({
960+
callback: async ({ action, result }) => {
961+
const saveResult = await logIntoSystem(action, result);
962962

963-
console.log(saveResult);
964-
},
965-
contentTypeName: "report",
966-
hookName: "afterCreate",
967-
});
963+
console.log(saveResult);
964+
},
965+
contentTypeName: "report",
966+
hookName: "afterCreate",
967+
});
968968
```
969969

970970
## 💬 FAQ
@@ -977,14 +977,13 @@ Example:
977977

978978
```ts
979979
module.exports = {
980-
'comments': { enabled: true },
981-
'graphql': { enabled: true },
980+
comments: { enabled: true },
981+
graphql: { enabled: true },
982982
};
983983
```
984984

985985
If you already got it, make sure that `comments` plugin is inserted before `graphql`. That should do the job.
986986

987-
988987
## 🤝 Contributing to the plugin
989988

990989
Feel free to fork and make a Pull Request to this plugin project. All the input is warmly welcome!

server/src/services/__tests__/common.service.test.ts

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -284,17 +284,21 @@ describe('common.service', () => {
284284
const strapi = getStrapi();
285285
const service = getService(strapi);
286286
const mockComments = [
287-
{ id: 1, content: 'Parent 1', threadOf: null },
288-
{ id: 2, content: 'Child 1', threadOf: 1 },
289-
{ id: 3, content: 'Child 2', threadOf: 1 },
290-
{ id: 4, content: 'Grandchild 1', threadOf: 2 },
287+
{ id: 1, content: "Parent 1", threadOf: null },
288+
{ id: 2, content: "Child 1", threadOf: "1" },
289+
{ id: 3, content: "Child 2", threadOf: "1" },
290+
{ id: 4, content: "Grandchild 1", threadOf: "2" },
291291
];
292292

293293
mockCommentRepository.findMany.mockResolvedValue(mockComments);
294294
caster<jest.Mock>(getOrderBy).mockReturnValue(['createdAt', 'desc']);
295-
mockCommentRepository.findWithCount.mockResolvedValue({
296-
results: mockComments,
297-
pagination: { total: 4 },
295+
mockCommentRepository.findWithCount.mockImplementation(async (args) => {
296+
const threadOf = args?.where?.threadOf?.$eq ?? null;
297+
const filtered = mockComments.filter((c) => c.threadOf === threadOf);
298+
return {
299+
results: filtered,
300+
pagination: { total: filtered.length },
301+
};
298302
});
299303
mockStoreRepository.getConfig.mockResolvedValue([]);
300304

@@ -334,18 +338,25 @@ describe('common.service', () => {
334338
const strapi = getStrapi();
335339
const service = getService(strapi);
336340
const mockComments = [
337-
{ id: 2, content: 'Child 1', threadOf: 1 },
338-
{ id: 3, content: 'Child 2', threadOf: 1 },
339-
{ id: 4, content: 'Grandchild 1', threadOf: 2 },
340-
{ id: 5, content: 'Grandchild 2', threadOf: 2 },
341-
{ id: 6, content: 'Grandchild 3', threadOf: 4 },
341+
{ id: 2, content: "Child 1", threadOf: "1" },
342+
{ id: 3, content: "Child 2", threadOf: "1" },
343+
{ id: 4, content: "Grandchild 1", threadOf: "2" },
344+
{ id: 5, content: "Grandchild 2", threadOf: "2" },
345+
{ id: 6, content: "Grandchild 3", threadOf: "4" },
342346
];
343347

344348
mockCommentRepository.findMany.mockResolvedValue(mockComments);
345349
caster<jest.Mock>(getOrderBy).mockReturnValue(['createdAt', 'desc']);
346-
mockCommentRepository.findWithCount.mockResolvedValue({
347-
results: mockComments,
348-
pagination: { total: 3 },
350+
mockCommentRepository.findWithCount.mockImplementation(async (args) => {
351+
const threadOf =
352+
args?.where?.threadOf?.$eq ??
353+
args?.where?.threadOf.toString() ??
354+
null;
355+
const filtered = mockComments.filter((c) => c.threadOf === threadOf);
356+
return {
357+
results: filtered,
358+
pagination: { total: filtered.length },
359+
};
349360
});
350361
mockStoreRepository.getConfig.mockResolvedValue([]);
351362

@@ -365,16 +376,20 @@ describe('common.service', () => {
365376
const service = getService(strapi);
366377
const mockComments = [
367378
{ id: 1, content: 'Parent 1', threadOf: null, dropBlockedThreads: true, blockedThread: true },
368-
{ id: 2, content: 'Child 1', threadOf: 1, dropBlockedThreads: false },
369-
{ id: 3, content: 'Child 2', threadOf: 1, dropBlockedThreads: false },
370-
{ id: 4, content: 'Grandchild 1', threadOf: 2, dropBlockedThreads: false },
379+
{ id: 2, content: 'Child 1', threadOf: '1', dropBlockedThreads: false },
380+
{ id: 3, content: 'Child 2', threadOf: '1', dropBlockedThreads: false },
381+
{ id: 4, content: 'Grandchild 1', threadOf: '2', dropBlockedThreads: false },
371382
];
372383

373384
mockCommentRepository.findMany.mockResolvedValue(mockComments);
374385
caster<jest.Mock>(getOrderBy).mockReturnValue(['createdAt', 'desc']);
375-
mockCommentRepository.findWithCount.mockResolvedValue({
376-
results: mockComments,
377-
pagination: { total: 4 },
386+
mockCommentRepository.findWithCount.mockImplementation(async (args) => {
387+
const threadOf = args?.where?.threadOf?.$eq ?? null;
388+
const filtered = mockComments.filter((c) => c.threadOf === threadOf);
389+
return {
390+
results: filtered,
391+
pagination: { total: filtered.length },
392+
};
378393
});
379394
mockStoreRepository.getConfig.mockResolvedValue([]);
380395

@@ -606,4 +621,4 @@ describe('common.service', () => {
606621
expect(mockCommentRepository.updateMany).toHaveBeenCalled();
607622
});
608623
});
609-
});
624+
});

0 commit comments

Comments
 (0)