Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

removed usage of any keyword from connection utilities, improved tsdoc comments #1886

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions src/utilities/graphQLConnection/generateDefaultGraphQLConnection.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
import type { ConnectionPageInfo } from "../../types/generatedGraphQLTypes";

/**
This is typescript type of a base graphQL connection edge object. This connection edge object
can be extended to create a custom connection edge object as long as the new connection edge
object adheres to the default type of this base connection edge object.
*/
* This is typescript type of a base graphQL connection edge object. This connection edge object
* can be extended to create a custom connection edge object as long as the new connection edge
* object adheres to the default type of this base connection edge object.
*/
export type DefaultGraphQLConnectionEdge<T0> = {
cursor: string;
node: T0;
};

/**
This is typescript type of a base graphQL connection object. This connection object can be
extended to create a custom connnection object as long as the new connection object adheres
to the default type of this base connection object.
*/
* This is typescript type of a base graphQL connection object. This connection object can be
* extended to create a custom connnection object as long as the new connection object adheres
* to the default type of this base connection object.
*/
export type DefaultGraphQLConnection<T0> = {
edges: DefaultGraphQLConnectionEdge<T0>[];
pageInfo: ConnectionPageInfo;
totalCount: number;
};

/**
This is a factory function to create a base graphql connection object with default fields
that correspond to a connection with no data and no traversal properties in any direction.
*/
* This is a factory function to create a base graphql connection object with default fields
* that correspond to a connection with no data and no traversal properties in any direction.
* @example
* const connection = generateDefaultGraphQLConnection();
*/
export function generateDefaultGraphQLConnection<
T0,
>(): DefaultGraphQLConnection<T0> {
Expand Down
40 changes: 26 additions & 14 deletions src/utilities/graphQLConnection/getCommonGraphQLConnectionFilter.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { GraphQLConnectionTraversalDirection } from "./index";

/**
This is typescript type of the object returned from function `getCommonGraphQLConnectionFilter`.
*/
* This is typescript type of the object returned from function `getCommonGraphQLConnectionFilter`.
*/
type CommonGraphQLConnectionFilter =
| {
_id: {
Expand All @@ -17,18 +17,30 @@ type CommonGraphQLConnectionFilter =
| Record<string, never>;

/**
This function is used to get an object containing common mongoose filtering logic. Here are a
few assumptions this function makes which are common to most of the graphQL connections.

The entity that has the latest creation datetime must appear at the top of the connection. This
means the default filtering logic would be to filter in descending order by the time of creation of
an entity, and if two or more entities have the same time of creation filtering in descending order
by the primary key of the entity. MongoDB object ids are lexographically sortable all on their own
because they contain information about both the creation time and primary key for the document.

Therefore, this function only returns filtering logic for filtering by the object id of a mongoDB
document.
*/
* This function is used to get an object containing common mongoose filtering logic.
*
* @remarks
*
* Here are a few assumptions this function makes which are common to most of the
* graphQL connections.
*
* The entity that has the latest creation datetime must appear at the top of the connection. This
* means the default filtering logic would be to filter in descending order by the time of creation of
* an entity, and if two or more entities have the same time of creation filtering in descending order
* by the primary key of the entity. MongoDB object ids are lexographically sortable all on their own
* because they contain information about both the creation time and primary key for the document.
*
* Therefore, this function only returns filtering logic for filtering by the object id of a mongoDB
* document.
*
* @example
*
* const filter = getCommonGraphQLConnectionFilter(\{
* cursor: "65da3f8df35eb5bfd52c5368",
* direction: "BACKWARD"
* \});
* const objectList = await User.find(filter).limit(10);
*/
export function getCommonGraphQLConnectionFilter({
cursor,
direction,
Expand Down
35 changes: 21 additions & 14 deletions src/utilities/graphQLConnection/getCommonGraphQLConnectionSort.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { GraphQLConnectionTraversalDirection } from "./index";

/**
This is typescript type of the object returned from `getCommonGraphQLConnectionSort` function.
*/
*This is typescript type of the object returned from `getCommonGraphQLConnectionSort` function.
*/
type CommmonGraphQLConnectionSort =
| {
_id: 1;
Expand All @@ -12,18 +12,25 @@ type CommmonGraphQLConnectionSort =
};

/**
This function is used to get an object containing common mongoose sorting logic. Here are a
few assumptions this function makes which are common to most of the graphQL connections.

The entity that has the latest creation datetime must appear at the top of the connection. This
means the default sorting logic would be sorting in descending order by the time of creation of
an entity, and if two or more entities have the same time of creation sorting in descending order
by the primary key of the entity. MongoDB object ids are lexographically sortable all on their own
because they contain information about both the creation time and primary key for the document.

Therefore, this function only returns sorting logic for sorting by the object id of a mongoDB
document.
*/
* This function is used to get an object containing common mongoose sorting logic.
* @remarks
* Here are a few assumptions this function makes which are common to most of the
* graphQL connections.
*
* The entity that has the latest creation datetime must appear at the top of the connection. This
* means the default sorting logic would be sorting in descending order by the time of creation of
* an entity, and if two or more entities have the same time of creation sorting in descending order
* by the primary key of the entity. MongoDB object ids are lexographically sortable all on their own
* because they contain information about both the creation time and primary key for the document.
*
* Therefore, this function only returns sorting logic for sorting by the object id of a mongoDB
* document.
* @example
* const sort = getCommonGraphQLConnectionSort(\{
* direction: "BACKWARD"
* \});
* const objectList = await User.find().sort(sort).limit(10);
*/
export function getCommonGraphQLConnectionSort({
direction,
}: {
Expand Down
36 changes: 22 additions & 14 deletions src/utilities/graphQLConnection/index.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
/**
This function is used to check nullish state of a value passed to it. Nullish means the
value being either `null` or `undefined`. If the value is found to be nullish, the function
returns the boolean `false`, else it returns the boolean `true`.
*/
* This function is used to check nullish state of a value passed to it. Nullish means the
* value being either `null` or `undefined`. If the value is found to be nullish, the function
* returns the boolean `false`, else it returns the boolean `true`.
* @example
* Here's an example:-
* function print(str: string | null) \{
* if(isNotNullish(str)) \{
* console.log(`the string is ${str}`)
* \} else \{
* console.log(`the string is null`)
* \}
* \}
*/
export function isNotNullish<T0>(value: T0 | undefined | null): value is T0 {
return value !== undefined && value !== null;
}

/**
This is typescript type of a base graphQL argument error. This argument error type can be
extended to create custom argument error types as long as they adhere to the default type of
this base graphQL argument error.
*/
* This is typescript type of a base graphQL argument error. This argument error type can be
* extended to create custom argument error types as long as they adhere to the default type of
* this base graphQL argument error.
*/
export type DefaultGraphQLArgumentError = {
message: string;
path: string[];
};

/**
This is typescript type of the standard arguments object received by a graphQL connection resolver
following the relay cursor connection specification, more info here:-
https://relay.dev/graphql/connections.htm
*/
* This is typescript type of the standard arguments object received by a graphQL connection
* following the relay cursor connection specification, more info here:- {@link https://relay.dev/graphql/connections.htm}
*/
export type DefaultGraphQLConnectionArguments = {
after?: string | null;
before?: string | null;
Expand All @@ -30,8 +38,8 @@ export type DefaultGraphQLConnectionArguments = {
};

/**
This is typescript type of direction the graphQL connection is to be traversed in.
*/
* This is typescript type of the direction the graphQL connection is to be traversed in.
*/
export type GraphQLConnectionTraversalDirection = "FORWARD" | "BACKWARD";

export * from "./generateDefaultGraphQLConnection";
Expand Down
51 changes: 35 additions & 16 deletions src/utilities/graphQLConnection/parseGraphQLConnectionArguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
} from "./index";

/**
This is typescript type of the single object callback function `parseCursor` takes in as an argument.
*This is typescript type of the single object callback function `parseCursor` takes in as
* an argument.
*/
export type ParseGraphQLConnectionCursorArguments = {
cursorName: "after" | "before";
Expand All @@ -16,11 +17,11 @@ export type ParseGraphQLConnectionCursorArguments = {
};

/**
This is typescript type of object returned from the callback function `parseCursor` passed
as an argument to `parseGraphQLConnectionArguments`, `parseGraphQLConnectionArgumentsWithSortedBy`,
`parseGraphQLConnectionArgumentsWithWhere` and `parseGraphQLConnectionArgumentsWithSortedByAndWhere`
functions.
*/
* This is typescript type of object returned from the callback function `parseCursor` passed
* as an argument to `parseGraphQLConnectionArguments`, `parseGraphQLConnectionArgumentsWithSortedBy`,
* `parseGraphQLConnectionArgumentsWithWhere` and `parseGraphQLConnectionArgumentsWithSortedByAndWhere`
* functions.
*/
export type ParseGraphQLConnectionCursorResult<T0> = Promise<
| {
errors: DefaultGraphQLArgumentError[];
Expand All @@ -33,26 +34,26 @@ export type ParseGraphQLConnectionCursorResult<T0> = Promise<
>;

/**
This is typescript type of the callback function `parseCursor`.
*/
* This is typescript type of the callback function `parseCursor`.
*/
export type ParseGraphQLConnectionCursor<T0> = (
args: ParseGraphQLConnectionCursorArguments,
) => ParseGraphQLConnectionCursorResult<T0>;

/**
This is typescript type of the object containing the validated and transformed connection
arguments passed to `parseGraphQLConnectionArguments` function.
*/
* This is typescript type of the object containing the validated and transformed connection
* arguments passed to `parseGraphQLConnectionArguments` function.
*/
export type ParsedGraphQLConnectionArguments<T0> = {
cursor: T0 | null;
direction: GraphQLConnectionTraversalDirection;
limit: number;
};

/**
This is typescript type of the object returned from `parseGraphQLConnectionArguments`
function.
*/
* This is typescript type of the object returned from `parseGraphQLConnectionArguments`
* function.
*/
export type ParseGraphQLConnectionArgumentsResult<T0> =
| {
errors: DefaultGraphQLArgumentError[];
Expand All @@ -64,8 +65,26 @@ export type ParseGraphQLConnectionArgumentsResult<T0> =
};

/**
This function handles validating and transforming arguments of a base graphQL connection.
*/
* This function handles validating and transforming arguments of a base graphQL connection.
* @example
* const result = await parseGraphQLConnectionArguments(\{
* args: \{
* after,
* first,
* \},
* maximumLimit: 20,
* parseCursor
* \})
* if (result.isSuccessful === false) \{
* throw new GraphQLError("Invalid arguments provided.", \{
* extensions: \{
* code: "INVALID_ARGUMENTS",
* errors: result.errors
* \}
* \})
* \}
* const \{ parsedArgs: \{ cursor, direction, limit \} \} = result;
*/
export async function parseGraphQLConnectionArguments<T0>({
args,
maximumLimit = MAXIMUM_FETCH_LIMIT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {
} from "./parseGraphQLConnectionArguments";

/**
This is typescript type of the object returned from callback function `parseSortedBy`.
*/
*This is typescript type of the object returned from callback function `parseSortedBy`.
*/
export type ParseGraphQLConnectionSortedByResult<T0> =
| {
isSuccessful: false;
Expand All @@ -23,24 +23,16 @@ export type ParseGraphQLConnectionSortedByResult<T0> =
};

/**
This is typescript type of the callback function `parseSortedBy`.
*/
export type ParseGraphQLConnectionSortedBy<T0> = (
/* eslint-disable @typescript-eslint/no-explicit-any */
...args: any[]
) => ParseGraphQLConnectionSortedByResult<T0>;

/**
This is typescript type of the object containing validated and transformed connection
arguments passed to `parseGraphQLConnectionArgumentsWithSortedBy` function.
*/
* This is typescript type of the object containing validated and transformed connection
* arguments passed to `parseGraphQLConnectionArgumentsWithSortedBy` function.
*/
export type ParsedGraphQLConnectionArgumentsWithSortedBy<T0, T1> = {
sort: T1;
} & ParsedGraphQLConnectionArguments<T0>;

/**
This is typescript type of the object returned from `parseGraphQLConnectionArgumentsWithSortedBy` function.
*/
* This is typescript type of the object returned from `parseGraphQLConnectionArgumentsWithSortedBy` function.
*/
export type ParseGraphQLConnectionArgumentsWithSortedByResult<T0, T1> = Promise<
| {
errors: DefaultGraphQLArgumentError[];
Expand All @@ -53,27 +45,45 @@ export type ParseGraphQLConnectionArgumentsWithSortedByResult<T0, T1> = Promise<
>;

/**
This function is used for validating and transforming arguments for a graphQL connection that
also provides sorting capabilities.
*/
* This function is used for validating and transforming arguments for a graphQL connection that
* also provides sorting capabilities.
* @example
* const result = await parseGraphQLConnectionArgumentsWithSortedBy(\{
* args: \{
* after,
* first,
* \},
* maximumLimit: 20,
* parseCursor,
* parseSortedBy,
* \})
* if (result.isSuccessful === false) \{
* throw new GraphQLError("Invalid arguments provided.", \{
* extensions: \{
* code: "INVALID_ARGUMENTS",
* errors: result.errors
* \}
* \})
* \}
* const \{ parsedArgs: \{ cursor, direction, limit, sort \} \} = result;
*/
export async function parseGraphQLConnectionArgumentsWithSortedBy<T0, T1>({
args,
maximumLimit = MAXIMUM_FETCH_LIMIT,
parseCursor,
parseSortedBy,
parseSortedByResult,
}: {
args: DefaultGraphQLConnectionArguments;
maximumLimit?: number;
parseCursor: ParseGraphQLConnectionCursor<T0>;
parseSortedBy: ParseGraphQLConnectionSortedBy<T1>;
parseSortedByResult: ParseGraphQLConnectionSortedByResult<T1>;
}): ParseGraphQLConnectionArgumentsWithSortedByResult<T0, T1> {
const parseGraphQLConnectionArgumentsResult =
await parseGraphQLConnectionArguments({
args,
parseCursor,
maximumLimit,
});
const parseSortedByResult = parseSortedBy();

if (!parseGraphQLConnectionArgumentsResult.isSuccessful) {
if (!parseSortedByResult.isSuccessful) {
Expand Down
Loading
Loading