Skip to content

Commit c0dfa8c

Browse files
committed
feat(perf): removed project queries from AllWorkspacesWithProjects
1 parent 415c19f commit c0dfa8c

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/resolvers/event.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,18 @@ module.exports = {
4141
* @param factories - factories for working with models
4242
* @return {Promise<UserModel[]> | null}
4343
*/
44-
async visitedBy({ visitedBy, projectId }, _args, { factories, user }) {
44+
async visitedBy({ visitedBy, projectId, workspaceId }, _args, { factories, user }) {
4545
/**
4646
* Crutch for Demo Workspace
47+
* Prefer workspaceId from parent if present to avoid extra project lookup
4748
*/
48-
const project = await factories.projectsFactory.findById(projectId);
49+
const resolvedWorkspaceId = workspaceId || (await (async () => {
50+
const project = await factories.projectsFactory.findById(projectId);
51+
52+
return project ? project.workspaceId.toString() : undefined;
53+
})());
4954

50-
if (project.workspaceId.toString() === '6213b6a01e6281087467cc7a') {
55+
if (resolvedWorkspaceId === '6213b6a01e6281087467cc7a') {
5156
return [ await factories.usersFactory.findById(user.id) ];
5257
}
5358

src/resolvers/project.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,20 @@ module.exports = {
460460

461461
const dailyEventsPortion = await factory.findDailyEventsPortion(limit, nextCursor, sort, filters, search);
462462

463+
/**
464+
* Pass workspaceId down to events so nested resolvers (like Event.visitedBy)
465+
* can avoid extra project lookups.
466+
*/
467+
if (dailyEventsPortion && Array.isArray(dailyEventsPortion.dailyEvents)) {
468+
dailyEventsPortion.dailyEvents = dailyEventsPortion.dailyEvents.map((item) => ({
469+
...item,
470+
event: {
471+
...item.event,
472+
workspaceId: project.workspaceId && project.workspaceId.toString ? project.workspaceId.toString() : project.workspaceId,
473+
},
474+
}));
475+
}
476+
463477
return dailyEventsPortion;
464478
},
465479

0 commit comments

Comments
 (0)