Skip to content

Commit 3b3c0dd

Browse files
committed
Optimize for use-case of only thread count
This is pre-mature optimization but it's a query that is called on every page load for our main detail pages. That's a hot path, so I'm taking the effort here.
1 parent be52e1a commit 3b3c0dd

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

src/components/comments/comment-thread.repository.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,18 @@ export class CommentThreadRepository extends DtoRepository(CommentThread) {
9898
.first();
9999
return result!; // result from paginate() will always have 1 row.
100100
}
101+
102+
async count(parent: ID) {
103+
const result = await this.db
104+
.query()
105+
.match([
106+
node('node', 'CommentThread'),
107+
relation('in', '', 'commentThread', ACTIVE),
108+
node('', 'BaseNode', { id: parent }),
109+
])
110+
.return<{ count: number }>('count(node) as count')
111+
.map('count')
112+
.first();
113+
return result!;
114+
}
101115
}

src/components/comments/comment.service.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,18 @@ export class CommentService {
153153
}
154154
}
155155

156+
async getThreadCount(parent: Commentable, session: Session) {
157+
const perms = await this.getPermissionsFromResource(parent, session);
158+
159+
// Do check here since we don't filter in the db query.
160+
// Will need to be updated with DB switch.
161+
if (!perms.can('read')) {
162+
return 0;
163+
}
164+
165+
return await this.repo.threads.count(parent.id);
166+
}
167+
156168
async listThreads(
157169
parent: Commentable,
158170
input: CommentThreadListInput,

src/components/comments/commentable.resolver.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { Parent, Query, ResolveField, Resolver } from '@nestjs/graphql';
1+
import { Info, Parent, Query, ResolveField, Resolver } from '@nestjs/graphql';
22
import {
3+
Fields,
34
ID,
45
IdArg,
6+
IsOnly,
57
ListArg,
68
LoggedInSession,
79
Resource,
@@ -31,7 +33,12 @@ export class CommentableResolver {
3133
@ListArg(CommentThreadListInput) input: CommentThreadListInput,
3234
@LoggedInSession() session: Session,
3335
@Loader(CommentThreadLoader) commentThreads: LoaderOf<CommentThreadLoader>,
34-
): Promise<CommentThreadList> {
36+
@Info(Fields, IsOnly(['total'])) onlyTotal: boolean,
37+
) {
38+
if (onlyTotal) {
39+
const total = await this.service.getThreadCount(parent, session);
40+
return { total };
41+
}
3542
const list = await this.service.listThreads(parent, input, session);
3643
commentThreads.primeAll(list.items);
3744
return list;

0 commit comments

Comments
 (0)