-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- https://github.com/octokit/plugin-paginate-graphql.js Signed-off-by: Lexus Drumgold <[email protected]>
- Loading branch information
1 parent
0ce0b1c
commit 52e66fd
Showing
34 changed files
with
678 additions
and
337 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* @file Test Type Definitions - GQLError | ||
* @module tests/types/GQLError | ||
*/ | ||
|
||
/** | ||
* Mock GraphQL error object. | ||
*/ | ||
type GQLError = { | ||
/** | ||
* Error description. | ||
*/ | ||
message: string | ||
|
||
/** | ||
* Error path segments. | ||
*/ | ||
path: string[] | ||
|
||
/** | ||
* Error type. | ||
*/ | ||
type: string | ||
} | ||
|
||
export type { GQLError as default } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* @file Test Type Definitions - GQLPayload | ||
* @module tests/types/GQLPayload | ||
*/ | ||
|
||
import type { Nullable } from '@flex-development/tutils' | ||
|
||
/** | ||
* GraphQL payload object. | ||
* | ||
* @template K - Payload data name | ||
* @template T - Payload data type | ||
*/ | ||
type GQLPayload<K extends string, T = any> = { | ||
/** | ||
* Payload data. | ||
*/ | ||
payload: Nullable<Record<K, T extends readonly any[] ? { nodes: T } : T>> | ||
} | ||
|
||
export type { GQLPayload as default } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/** | ||
* @file Test Utilities - GQLResponse | ||
* @module tests/utils/GQLResponse | ||
*/ | ||
|
||
import data from '#fixtures/api.github.com/graphql.json' assert { type: 'json' } | ||
import type { GQLError } from '#tests/types' | ||
import { | ||
at, | ||
isNull, | ||
select, | ||
type Nullable, | ||
type ObjectCurly | ||
} from '@flex-development/tutils' | ||
import type { GQLPaginated } from '@octokit/plugin-paginate-graphql' | ||
import { HttpResponse, type StrictResponse } from 'msw' | ||
|
||
/** | ||
* Paginated data names. | ||
*/ | ||
type PaginateKey = Exclude<keyof typeof data.data.repository, 'id'> | ||
|
||
/** | ||
* Pagination options. | ||
*/ | ||
type PaginateOptions = { | ||
/** | ||
* Cursor of last node. | ||
* | ||
* @default null | ||
*/ | ||
cursor?: Nullable<string> | ||
|
||
/** | ||
* Paginated data key. | ||
*/ | ||
key: PaginateKey | ||
} | ||
|
||
/** | ||
* Paginated data object. | ||
* | ||
* @template K - Paginated data name | ||
* @template T - Paginated data type | ||
*/ | ||
type PaginatedDataObject<K extends string, T extends ObjectCurly> = | ||
| { data: { payload: null }; errors: GQLError[] } | ||
| { data: GQLPaginated<K, T> } | ||
|
||
/** | ||
* GraphQL-specific response object mock. | ||
* | ||
* @see {@linkcode HttpResponse} | ||
* | ||
* @class | ||
* @extends {HttpResponse} | ||
*/ | ||
class GQLResponse extends HttpResponse { | ||
/** | ||
* Create a paginated {@linkcode Response}. | ||
* | ||
* @public | ||
* @static | ||
* | ||
* @template K - Paginated data name | ||
* @template T - Paginated data type | ||
* | ||
* @param {PaginateOptions} options - Pagination options | ||
* @return {StrictResponse<PaginatedDataObject<K, T>>} Paginated response | ||
*/ | ||
public static paginate< | ||
K extends PaginateKey, | ||
T extends ObjectCurly | ||
>(options: PaginateOptions): StrictResponse<PaginatedDataObject<K, T>> { | ||
const { cursor = null, key } = options | ||
const { edges, nodes } = data.data.repository[key] | ||
|
||
/** | ||
* Index of current edge. | ||
* | ||
* @const {number} i | ||
*/ | ||
const i: number = isNull(cursor) | ||
? 0 | ||
: select(edges, null, e => e.cursor).indexOf(cursor) + 1 | ||
|
||
// return error response if cursor is invalid | ||
if (i === -1) { | ||
return this.json({ | ||
data: { payload: null }, | ||
errors: [ | ||
{ | ||
locations: [{ column: -1, line: -1 }], | ||
message: `\`${cursor}\` does not appear to be a valid cursor.`, | ||
path: ['payload', key, 'edges'], | ||
type: 'INVALID_CURSOR_ARGUMENTS' | ||
} | ||
] | ||
}) | ||
} | ||
|
||
/** | ||
* Index of next edge. | ||
* | ||
* @var {number} j | ||
*/ | ||
const j: number = i + 10 | ||
|
||
/** | ||
* Cursors from current edge to next edge. | ||
* | ||
* @const {{ cursor: string }[]} cursors | ||
*/ | ||
const cursors: { cursor: string }[] = edges.slice(i, j) | ||
|
||
return this.json({ | ||
data: { | ||
payload: <T>{ | ||
[key]: { | ||
nodes: nodes.slice(i, j), | ||
pageInfo: { | ||
endCursor: at(cursors, -1)?.cursor ?? cursor, | ||
hasNextPage: !!cursors.length | ||
} | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
export default GQLResponse |
Oops, something went wrong.