Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
meetulr committed Nov 2, 2024
1 parent f60d605 commit f345bdd
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,25 @@ import type {
/**
* This is typescript type of the object returned from function `getUserTagGraphQLConnectionFilter`.
*/
type UserTagGraphQLConnectionFilter =
| {
_id?: {
$lt: string;
};
name: {
$regex: RegExp;
};
}
| {
_id?: {
$gt: string;
};
name: {
$regex: RegExp;
};
};
type BaseUserTagGraphQLConnectionFilter = {
name: {
$regex: RegExp;
};
};

type UserTagGraphQLConnectionFilter = BaseUserTagGraphQLConnectionFilter &
(
| {
_id?: {
$lt: string;
};
}
| {
_id?: {
$gt: string;
};
}
);
/**
* This function is used to get an object containing filtering logic.
*/
Expand All @@ -48,28 +49,19 @@ export function getUserTagGraphQLConnectionFilter({
};

if (cursor !== null) {
if (sortById === "ASCENDING") {
if (direction === "BACKWARD") {
filter._id = {
$lt: cursor,
};
} else {
filter._id = {
$gt: cursor,
};
}
} else {
if (direction === "BACKWARD") {
filter._id = {
$gt: cursor,
};
} else {
filter._id = {
$lt: cursor,
};
}
}
filter._id = getCursorFilter(cursor, sortById, direction);
}

return filter;
}

function getCursorFilter(
cursor: string,
sortById: "ASCENDING" | "DESCENDING",
direction: GraphQLConnectionTraversalDirection,
): { $lt: string } | { $gt: string } {
if (sortById === "ASCENDING") {
return direction === "BACKWARD" ? { $lt: cursor } : { $gt: cursor };
}
return direction === "BACKWARD" ? { $gt: cursor } : { $lt: cursor };
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,29 @@ import type {
/**
* This is typescript type of the object returned from function `getUserTagMemberGraphQLConnectionFilter`.
*/
type BaseUserTagMemberGraphQLConnectionFilter = {
firstName: {
$regex: RegExp;
};
lastName: {
$regex: RegExp;
};
};

type UserTagMemberGraphQLConnectionFilter =
| {
_id?: {
$lt: Types.ObjectId;
};
firstName: {
$regex: RegExp;
};
lastName: {
$regex: RegExp;
};
}
| {
_id?: {
$gt: Types.ObjectId;
};
firstName: {
$regex: RegExp;
};
lastName: {
$regex: RegExp;
};
};
BaseUserTagMemberGraphQLConnectionFilter &
(
| {
_id?: {
$lt: Types.ObjectId;
};
}
| {
_id?: {
$gt: Types.ObjectId;
};
}
);

/**
* This function is used to get an object containing filtering logic.
Expand Down Expand Up @@ -62,28 +62,20 @@ export function getUserTagMemberGraphQLConnectionFilter({
};

if (cursor !== null) {
if (sortById === "ASCENDING") {
if (direction === "BACKWARD") {
filter._id = {
$lt: new Types.ObjectId(cursor),
};
} else {
filter._id = {
$gt: new Types.ObjectId(cursor),
};
}
} else {
if (direction === "BACKWARD") {
filter._id = {
$gt: new Types.ObjectId(cursor),
};
} else {
filter._id = {
$lt: new Types.ObjectId(cursor),
};
}
}
filter._id = getCursorFilter(cursor, sortById, direction);
}

return filter;
}

function getCursorFilter(
cursor: string,
sortById: "ASCENDING" | "DESCENDING",
direction: GraphQLConnectionTraversalDirection,
): { $lt: Types.ObjectId } | { $gt: Types.ObjectId } {
const cursorId = new Types.ObjectId(cursor);
if (sortById === "ASCENDING") {
return direction === "BACKWARD" ? { $lt: cursorId } : { $gt: cursorId };
}
return direction === "BACKWARD" ? { $gt: cursorId } : { $lt: cursorId };
}

0 comments on commit f345bdd

Please sign in to comment.