Skip to content

Commit

Permalink
improve action presence data loader arg params
Browse files Browse the repository at this point in the history
no more need for string data packing, just use a clean
interface type with the two needed args.
  • Loading branch information
nick-funk committed Nov 17, 2023
1 parent 20d2bbe commit 5646ac2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 32 deletions.
56 changes: 27 additions & 29 deletions server/src/core/server/graph/loaders/Comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ const mapVisibleComment = (user?: Pick<User, "role">) => {
};
};

interface ActionPresenceArgs {
commentID: string;
isArchived: boolean;
}

/**
* mapVisibleComments will map each comment an array to an array of Comment and
* null.
Expand Down Expand Up @@ -250,38 +255,31 @@ export default (ctx: GraphContext) => ({
isArchived
).then(primeCommentsFromConnection(ctx));
},
retrieveMyActionPresence: new DataLoader<string, GQLActionPresence>(
async (stateIDs: string[]) => {
if (!ctx.user) {
// This should only ever be accessed when a user is logged in. It should
// be safe to get the user here, but we'll throw an error anyways just
// in case.
throw new Error("can't get action presence of an undefined user");
}

const requestData = stateIDs.map((id) => {
const split = id.split(":");
return {
commentID: split[0],
isArchived: split[1] === "true",
};
});
retrieveMyActionPresence: new DataLoader<
ActionPresenceArgs,
GQLActionPresence
>(async (args: ActionPresenceArgs[]) => {
if (!ctx.user) {
// This should only ever be accessed when a user is logged in. It should
// be safe to get the user here, but we'll throw an error anyways just
// in case.
throw new Error("can't get action presence of an undefined user");
}

const commentIDs = requestData.map((rd) => rd.commentID);
const hasArchivedData = requestData.some((rd) => rd.isArchived);
const commentIDs = args.map((rd) => rd.commentID);
const hasArchivedData = args.some((rd) => rd.isArchived);

const result = await retrieveManyUserActionPresence(
ctx.mongo,
ctx.cache.commentActions,
ctx.tenant.id,
ctx.user.id,
commentIDs,
hasArchivedData
);
const result = await retrieveManyUserActionPresence(
ctx.mongo,
ctx.cache.commentActions,
ctx.tenant.id,
ctx.user.id,
commentIDs,
hasArchivedData
);

return result;
}
),
return result;
}),
forUser: (userID: string, { first, orderBy, after }: UserToCommentsArgs) =>
retrieveCommentUserConnection(ctx.mongo, ctx.tenant.id, userID, {
first: defaultTo(first, 10),
Expand Down
7 changes: 4 additions & 3 deletions server/src/core/server/graph/resolvers/Comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,10 @@ export const Comment: GQLCommentTypeResolver<comment.Comment> = {
throw new StoryNotFoundError(c.storyID);
}

return ctx.loaders.Comments.retrieveMyActionPresence.load(
`${c.id}:${!!story.isArchived}`
);
return ctx.loaders.Comments.retrieveMyActionPresence.load({
commentID: c.id,
isArchived: !!story.isArchived,
});
},

parentCount: (c) => getDepth(c),
Expand Down

0 comments on commit 5646ac2

Please sign in to comment.