Skip to content

Commit

Permalink
feat(lib): return empty response when passed undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
good-idea committed Oct 18, 2019
1 parent c3e5148 commit c1e7eb9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/__tests__/unwindEdges.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ describe('unwindEdges', () => {
expect(lastCursor).toBe(thirdUser.cursor)
})

it('should return an empty array when given an undefined value', () => {
const [edges, { pageInfo }] = unwindEdges(undefined)
expect(edges.length).toBe(0)
expect(pageInfo.hasNextPage).toBe(false)
expect(pageInfo.hasPreviousPage).toBe(false)
})

it('should accept an object without any pageInfo', () => {
const edgesNoInfo = {
edges: sampleResponse.allUsers.edges,
Expand Down
17 changes: 16 additions & 1 deletion src/unwindEdges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,22 @@ import { UnwoundEdges, Paginated } from './types'
* @returns {UnwoundEdges<EdgeType>}
*/

export const unwindEdges = <EdgeType = any>({ edges, pageInfo }: Paginated<EdgeType>): UnwoundEdges<EdgeType> => {
const emptyResponse: UnwoundEdges<any> = [
[],
{
pageInfo: {
hasNextPage: false,
hasPreviousPage: false,
},
firstCursor: undefined,
lastCursor: undefined,
},
]

export const unwindEdges = <EdgeType = any>(paginated?: Paginated<EdgeType>): UnwoundEdges<EdgeType> => {
if (!paginated) return emptyResponse
const edges = paginated.edges || []
const { pageInfo } = paginated
return [
edges.map((edge) => ({ ...edge.node, __cursor: edge.cursor })),
{
Expand Down

0 comments on commit c1e7eb9

Please sign in to comment.