Skip to content

Commit

Permalink
add error handling to parseWhere and parseSortedBy
Browse files Browse the repository at this point in the history
  • Loading branch information
meetulr committed Oct 31, 2024
1 parent d4858a3 commit a7fb289
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 122 deletions.
16 changes: 5 additions & 11 deletions src/resolvers/Organization/userTags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,14 @@ export const userTags: OrganizationResolvers["userTags"] = async (
parent,
args,
) => {
const parsedWhere = parseUserTagWhere(args.where);
const parsedSortedBy = parseUserTagSortedBy(args.sortedBy);
const parseWhereResult = parseUserTagWhere(args.where);
const parseSortedByResult = parseUserTagSortedBy(args.sortedBy);

const parseGraphQLConnectionArgumentsResult =
await parseGraphQLConnectionArgumentsWithSortedByAndWhere({
args,
parseSortedByResult: {
isSuccessful: true,
parsedSortedBy,
},
parseWhereResult: {
isSuccessful: true,
parsedWhere,
},
parseSortedByResult,
parseWhereResult,
parseCursor: (args) =>
parseCursor({
...args,
Expand Down Expand Up @@ -94,7 +88,7 @@ export const userTags: OrganizationResolvers["userTags"] = async (

// if there's no search input, we'll list all the root tag
// otherwise we'll also list the subtags matching the filter
const parentTagIdFilter = parsedWhere.nameStartsWith
const parentTagIdFilter = parsedArgs.filter.nameStartsWith
? {}
: { parentTagId: null };

Expand Down
14 changes: 4 additions & 10 deletions src/resolvers/UserTag/childTags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,14 @@ export const childTags: UserTagResolvers["childTags"] = async (
parent,
args,
) => {
const parsedWhere = parseUserTagWhere(args.where);
const parsedSortedBy = parseUserTagSortedBy(args.sortedBy);
const parseWhereResult = parseUserTagWhere(args.where);
const parseSortedByResult = parseUserTagSortedBy(args.sortedBy);

const parseGraphQLConnectionArgumentsResult =
await parseGraphQLConnectionArgumentsWithSortedByAndWhere({
args,
parseSortedByResult: {
isSuccessful: true,
parsedSortedBy,
},
parseWhereResult: {
isSuccessful: true,
parsedWhere,
},
parseSortedByResult,
parseWhereResult,
parseCursor: (args) =>
parseCursor({
...args,
Expand Down
11 changes: 4 additions & 7 deletions src/resolvers/UserTag/usersAssignedTo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,14 @@ export const usersAssignedTo: UserTagResolvers["usersAssignedTo"] = async (
parent,
args,
) => {
const parsedWhere = parseUserTagUserWhere(args.where);
const parsedSortedBy = parseUserTagSortedBy(args.sortedBy);
const parseWhereResult = parseUserTagUserWhere(args.where);
const parseSortedByResult = parseUserTagSortedBy(args.sortedBy);

const parseGraphQLConnectionArgumentsResult =
await parseGraphQLConnectionArgumentsWithSortedByAndWhere({
args,
parseSortedByResult: {
isSuccessful: true,
parsedSortedBy,
},
parseWhereResult: parsedWhere,
parseSortedByResult,
parseWhereResult,
parseCursor: (args) =>
parseCursor({
...args,
Expand Down
44 changes: 2 additions & 42 deletions src/resolvers/UserTag/usersToAssignTo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type { InterfaceUser } from "../../models";
import { User } from "../../models";
import type {
DefaultGraphQLArgumentError,
GraphQLConnectionTraversalDirection,
ParseGraphQLConnectionCursorArguments,
ParseGraphQLConnectionCursorResult,
} from "../../utilities/graphQLConnection";
Expand Down Expand Up @@ -41,12 +40,12 @@ export const usersToAssignTo: UserTagResolvers["usersToAssignTo"] = async (
parent,
args,
) => {
const parsedWhere = parseUserTagUserWhere(args.where);
const parseWhereResult = parseUserTagUserWhere(args.where);

const parseGraphQLConnectionArgumentsResult =
await parseGraphQLConnectionArgumentsWithWhere({
args,
parseWhereResult: parsedWhere,
parseWhereResult,
parseCursor: (args) =>
parseCursor({
...args,
Expand Down Expand Up @@ -195,42 +194,3 @@ export const parseCursor = async ({
parsedCursor: cursorValue,
};
};

type GraphQLConnectionFilter =
| {
_id: {
$lt: Types.ObjectId;
};
}
| {
_id: {
$gt: Types.ObjectId;
};
}
| Record<string, never>;

export const getGraphQLConnectionFilter = ({
cursor,
direction,
}: {
cursor: string | null;
direction: GraphQLConnectionTraversalDirection;
}): GraphQLConnectionFilter => {
if (cursor !== null) {
if (direction === "BACKWARD") {
return {
_id: {
$gt: new Types.ObjectId(cursor),
},
};
} else {
return {
_id: {
$lt: new Types.ObjectId(cursor),
},
};
}
} else {
return {};
}
};
29 changes: 26 additions & 3 deletions src/utilities/userTagsUtils/parseUserTagSortedBy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import type {
SortedByOrder,
UserTagSortedByInput,
} from "../../types/generatedGraphQLTypes";
import type {
DefaultGraphQLArgumentError,
ParseGraphQLConnectionSortedByResult,
} from "../graphQLConnection";

/*
* function to parse the args.sortedBy for UserTag queries
Expand All @@ -13,14 +17,33 @@ export type ParseSortedByResult = {

export function parseUserTagSortedBy(
sortedBy: UserTagSortedByInput | null | undefined,
): ParseSortedByResult {
): ParseGraphQLConnectionSortedByResult<ParseSortedByResult> {
const errors: DefaultGraphQLArgumentError[] = [];

if (!sortedBy) {
return {
sortById: "DESCENDING",
isSuccessful: true,
parsedSortedBy: { sortById: "DESCENDING" },
};
} else {
if (sortedBy.id !== "DESCENDING" && sortedBy.id !== "ASCENDING") {
errors.push({
message:
"Invalid sortedByInput provided. It must be a of type SortedByOrder.",
path: ["sortUserTagInput"],
});

return {
isSuccessful: false,
errors,
};
}

return {
sortById: sortedBy.id,
isSuccessful: true,
parsedSortedBy: {
sortById: sortedBy.id,
},
};
}
}
2 changes: 1 addition & 1 deletion src/utilities/userTagsUtils/parseUserTagUserWhere.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function parseUserTagUserWhere(
if (!where.firstName && !where.lastName) {
errors.push({
message: `Atleast one of firstName or lastName should be provided`,
path: ["whereInput"],
path: ["whereUserNameInput"],
});

return {
Expand Down
34 changes: 29 additions & 5 deletions src/utilities/userTagsUtils/parseUserTagWhere.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import type { UserTagWhereInput } from "../../types/generatedGraphQLTypes";
import type {
DefaultGraphQLArgumentError,
ParseGraphQLConnectionWhereResult,
} from "../graphQLConnection";

/*
* function to parse the args.where for UserTag queries
Expand All @@ -10,14 +14,34 @@ export type ParseUserTagWhereResult = {

export function parseUserTagWhere(
where: UserTagWhereInput | null | undefined,
): ParseUserTagWhereResult {
): ParseGraphQLConnectionWhereResult<ParseUserTagWhereResult> {
const errors: DefaultGraphQLArgumentError[] = [];

if (!where) {
return {
nameStartsWith: "",
isSuccessful: true,
parsedWhere: {
nameStartsWith: "",
},
};
} else {
return {
nameStartsWith: where.name.starts_with.trim(),
};
if (typeof where.name.starts_with !== "string") {
errors.push({
message: "Invalid name provided. It must be a string.",
path: ["whereUserTagNameInput"],
});

return {
isSuccessful: false,
errors,
};
} else {
return {
isSuccessful: true,
parsedWhere: {
nameStartsWith: where.name.starts_with.trim(),
},
};
}
}
}
43 changes: 0 additions & 43 deletions tests/resolvers/UserTag/usersToAssignTo.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import "dotenv/config";
import {
parseCursor,
getGraphQLConnectionFilter,
usersToAssignTo as usersToAssignToResolver,
} from "../../../src/resolvers/UserTag/usersToAssignTo";
import { connect, disconnect } from "../../helpers/db";
Expand Down Expand Up @@ -116,44 +114,3 @@ describe("parseCursor function", () => {
}
});
});

describe("getGraphQLConnectionFilter function", () => {
it(`when argument cursor is non-null and argument direction corresponds to backward`, async () => {
const cursor = new Types.ObjectId().toString();

expect(
getGraphQLConnectionFilter({
cursor,
direction: "BACKWARD",
}),
).toEqual({
_id: {
$gt: new Types.ObjectId(cursor),
},
});
});

it(`when argument cursor is non-null and argument direction corresponds to forward`, async () => {
const cursor = new Types.ObjectId().toString();

expect(
getGraphQLConnectionFilter({
cursor,
direction: "FORWARD",
}),
).toEqual({
_id: {
$lt: new Types.ObjectId(cursor),
},
});
});

it(`when argument cursor is null`, async () => {
expect(
getGraphQLConnectionFilter({
cursor: null,
direction: "BACKWARD",
}),
).toEqual({});
});
});

0 comments on commit a7fb289

Please sign in to comment.