-
Notifications
You must be signed in to change notification settings - Fork 6
fix(api-gateway): graphql relay connection findAll do not work #126
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
Open
boomspeed94
wants to merge
4
commits into
lecle:develop
Choose a base branch
from
boomspeed94:fix/api-gateway-graphql-relay-findAll
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
04e3c1d
fix(api-gateway): :bug: graphql relay connection findAll do not work …
boomspeed94 d93c71b
fix(api-gateway): :bug: use connection name as prefix when create cur…
boomspeed94 20636e8
fix(api-gateway): :white_check_mark: add unit test (#120)
boomspeed94 e09a50c
fix(api-gateway): :white_check_mark: enhance unit test of graphql-com…
boomspeed94 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,121 @@ | ||
| import { Connection, ConnectionCursor } from 'graphql-relay'; | ||
| import type { QueryInput } from '@aloxide/demux'; | ||
|
|
||
| export interface PageInfoInput { | ||
| entityName: string; | ||
| edges: any[]; | ||
| limit: number; | ||
| offset?: string; | ||
| } | ||
|
|
||
| const NUMER_OF_ITEMS_PER_PAGE = 20; | ||
|
|
||
| export type Base64String = string; | ||
|
|
||
| export function base64(i: string): Base64String { | ||
| return Buffer.from(i, 'utf8').toString('base64'); | ||
| } | ||
|
|
||
| export function unbase64(i: Base64String): string { | ||
| return Buffer.from(i, 'base64').toString('utf8'); | ||
| } | ||
|
|
||
| /** | ||
| * Creates the cursor string from an offset. | ||
| */ | ||
| export function offsetToCursor(prefix: string, offset: number): ConnectionCursor { | ||
| return base64(prefix + offset); | ||
| } | ||
| /** | ||
| * Rederives the offset from the cursor string. | ||
| */ | ||
| export function cursorToOffset(prefix: string, cursor: ConnectionCursor): number { | ||
| if (cursor) return parseInt(unbase64(cursor).substring(prefix.length), 10); | ||
| return NaN; | ||
| } | ||
|
|
||
| export function convertCursorToOffet(entityName: string, args: any): QueryInput { | ||
| const updatedArgs: QueryInput = { ...args }; | ||
|
|
||
| const beforOffset = cursorToOffset(entityName, updatedArgs.before); | ||
| const afterOffset = cursorToOffset(entityName, updatedArgs.after); | ||
|
|
||
| if (isNaN(beforOffset)) delete updatedArgs.before; | ||
| else updatedArgs.before = beforOffset.toString(); | ||
|
|
||
| if (isNaN(afterOffset)) delete updatedArgs.after; | ||
| else updatedArgs.after = afterOffset.toString(); | ||
|
|
||
| if (updatedArgs.first) updatedArgs.first += 1; | ||
| if (updatedArgs.last) updatedArgs.last += 1; | ||
|
|
||
| return updatedArgs; | ||
| } | ||
|
|
||
| export function paginationInfo<T>( | ||
| entityName: string, | ||
| items: any[], | ||
| args: QueryInput, | ||
| ): Connection<T> { | ||
| if (args.first) { | ||
| return forwardPaginationInfo({ | ||
| entityName, | ||
| edges: items, | ||
| limit: args.first, | ||
| offset: args.after, | ||
| }); | ||
| } | ||
|
|
||
| if (args.last) { | ||
| return backwardPaginationInfo({ | ||
| entityName, | ||
| edges: items, | ||
| limit: args.last, | ||
| offset: args.before, | ||
| }); | ||
| } | ||
|
|
||
| return forwardPaginationInfo({ entityName, edges: items, limit: NUMER_OF_ITEMS_PER_PAGE }); | ||
| } | ||
|
|
||
| export function forwardPaginationInfo<T>(pageInput: PageInfoInput): Connection<T> { | ||
| let hasNextPage = false; | ||
| if (pageInput.edges.length > pageInput.limit) { | ||
| pageInput.edges = pageInput.edges.slice(0, pageInput.limit); | ||
| hasNextPage = true; | ||
| } | ||
| const edges = pageInput.edges.map(value => ({ | ||
| cursor: offsetToCursor(pageInput.entityName, value.id), | ||
| node: value, | ||
| })); | ||
| return { | ||
| edges, | ||
| pageInfo: { | ||
| startCursor: edges[0]?.cursor, | ||
| endCursor: edges[edges.length - 1]?.cursor, | ||
| hasNextPage, | ||
| hasPreviousPage: pageInput.offset ? true : false, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| export function backwardPaginationInfo<T>(pageInput: PageInfoInput): Connection<T> { | ||
| let hasPreviousPage = false; | ||
| if (pageInput.edges.length > pageInput.limit) { | ||
| pageInput.edges = pageInput.edges.slice(0, pageInput.limit); | ||
| hasPreviousPage = true; | ||
| } | ||
| const edges = pageInput.edges.reverse().map(value => ({ | ||
| cursor: offsetToCursor(pageInput.entityName, value.id), | ||
| node: value, | ||
| })); | ||
| return { | ||
| edges, | ||
| pageInfo: { | ||
| startCursor: edges[0]?.cursor, | ||
| endCursor: edges[edges.length - 1]?.cursor, | ||
| hasNextPage: pageInput.offset ? true : false, | ||
| hasPreviousPage, | ||
| }, | ||
| }; | ||
| } |
This file contains hidden or 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
167 changes: 167 additions & 0 deletions
167
packages/api-gateway/test/graphql-common-ultils.test.ts
This file contains hidden or 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,167 @@ | ||
| import { QueryInput } from '@aloxide/demux/src'; | ||
| import { | ||
| base64, | ||
| unbase64, | ||
| offsetToCursor, | ||
| cursorToOffset, | ||
| convertCursorToOffet, | ||
| forwardPaginationInfo, | ||
| backwardPaginationInfo, | ||
| paginationInfo, | ||
| } from '../src/graphql-common-utils'; | ||
|
|
||
| describe('Graphql Common Ultils', () => { | ||
| const entityName = 'test'; | ||
|
|
||
| it('Should convert to base64', () => { | ||
| expect(base64('hello')).toBe('aGVsbG8='); | ||
| }); | ||
|
|
||
| it('Should revert to string', () => { | ||
| expect(unbase64('aGVsbG8=')).toBe('hello'); | ||
| }); | ||
|
|
||
| it('Should parse offset to cursor', () => { | ||
| expect(offsetToCursor(entityName, 1)).toBe('dGVzdDE='); | ||
| }); | ||
|
|
||
| it('Should revert cursor to offset', () => { | ||
| expect(cursorToOffset(entityName, 'dGVzdDE=')).toBe(1); | ||
| }); | ||
|
|
||
| it('Revert offset Should return NaN', () => { | ||
| expect(cursorToOffset(entityName, 'hello')).toBeNaN(); | ||
| }); | ||
|
|
||
| it('Should convert cursor to offset', () => { | ||
| const input: QueryInput = { | ||
| first: 2, | ||
| after: 'test', | ||
| last: 2, | ||
| before: 'test', | ||
| }; | ||
|
|
||
| const resp = convertCursorToOffet(entityName, input); | ||
| expect(resp.first).toBe(3); | ||
| expect(resp.last).toBe(3); | ||
| expect(resp.after).toBeUndefined(); | ||
| expect(resp.before).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('Should convert cursor to offset', () => { | ||
| const input: QueryInput = { | ||
| first: 2, | ||
| after: 'dGVzdDE=', | ||
| last: 2, | ||
| before: 'dGVzdDE=', | ||
| }; | ||
|
|
||
| const resp = convertCursorToOffet(entityName, input); | ||
| expect(resp.first).toBe(3); | ||
| expect(resp.last).toBe(3); | ||
| expect(resp.after).toBe('1'); | ||
| expect(resp.before).toBe('1'); | ||
| }); | ||
|
|
||
| it('Should forward pagination', () => { | ||
| const items = [ | ||
| { | ||
| id: 1, | ||
| }, | ||
| { | ||
| id: 2, | ||
| }, | ||
| { | ||
| id: 3, | ||
| }, | ||
| ]; | ||
|
|
||
| const resp = paginationInfo(entityName, items, { first: 2 }); | ||
|
|
||
| expect(resp.edges).toBeDefined(); | ||
| expect(resp.edges[0].cursor).toBe('dGVzdDE='); | ||
| expect(resp.pageInfo.startCursor).toBe('dGVzdDE='); | ||
| expect(resp.pageInfo.endCursor).toBe('dGVzdDI='); | ||
| expect(resp.pageInfo.hasNextPage).toBe(true); | ||
| expect(resp.pageInfo.hasPreviousPage).toBe(false); | ||
| }); | ||
|
|
||
| it('Should forward pagination with after', () => { | ||
| const items = [ | ||
| { | ||
| id: 1, | ||
| }, | ||
| { | ||
| id: 2, | ||
| }, | ||
| { | ||
| id: 3, | ||
| }, | ||
| ]; | ||
|
|
||
| const resp = paginationInfo(entityName, items, { first: 2, after: 'test' }); | ||
|
|
||
| expect(resp.edges).toBeDefined(); | ||
| expect(resp.edges[0].cursor).toBe('dGVzdDE='); | ||
| expect(resp.pageInfo.startCursor).toBe('dGVzdDE='); | ||
| expect(resp.pageInfo.endCursor).toBe('dGVzdDI='); | ||
| expect(resp.pageInfo.hasNextPage).toBe(true); | ||
| expect(resp.pageInfo.hasPreviousPage).toBe(true); | ||
| }); | ||
|
|
||
| it('Should backward pagination', () => { | ||
| const items = [ | ||
| { | ||
| id: 37, | ||
| }, | ||
| { | ||
| id: 36, | ||
| }, | ||
| { | ||
| id: 35, | ||
| }, | ||
| ]; | ||
|
|
||
| const resp = paginationInfo(entityName, items, { last: 2 }); | ||
|
|
||
| expect(resp.edges).toBeDefined(); | ||
| expect(resp.edges[0].cursor).toBe('dGVzdDM2'); | ||
| expect(resp.pageInfo.startCursor).toBe('dGVzdDM2'); | ||
| expect(resp.pageInfo.endCursor).toBe('dGVzdDM3'); | ||
| expect(resp.pageInfo.hasNextPage).toBe(false); | ||
| expect(resp.pageInfo.hasPreviousPage).toBe(true); | ||
| }); | ||
|
|
||
| it('Should backward pagination with before', () => { | ||
| const items = [ | ||
| { | ||
| id: 37, | ||
| }, | ||
| { | ||
| id: 36, | ||
| }, | ||
| { | ||
| id: 35, | ||
| }, | ||
| ]; | ||
|
|
||
| const resp = paginationInfo(entityName, items, { last: 2, before: 'test' }); | ||
|
|
||
| expect(resp.edges).toBeDefined(); | ||
| expect(resp.edges[0].cursor).toBe('dGVzdDM2'); | ||
| expect(resp.pageInfo.startCursor).toBe('dGVzdDM2'); | ||
| expect(resp.pageInfo.endCursor).toBe('dGVzdDM3'); | ||
| expect(resp.pageInfo.hasNextPage).toBe(true); | ||
| expect(resp.pageInfo.hasPreviousPage).toBe(true); | ||
| }); | ||
|
|
||
| it('Should backward pagination by emtpy items', () => { | ||
| const resp = paginationInfo(entityName, [], { last: 2, before: 'test' }); | ||
|
|
||
| expect(resp.edges).toBeDefined(); | ||
| expect(resp.pageInfo.startCursor).toBeUndefined(); | ||
| expect(resp.pageInfo.endCursor).toBeUndefined(); | ||
| expect(resp.pageInfo.hasNextPage).toBe(true); | ||
| expect(resp.pageInfo.hasPreviousPage).toBe(false); | ||
| }); | ||
| }); | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really understand why this is 3 but not 2.