From 5f3ea722fc33c8cbf89cf78ffa3f496fdb96a6af Mon Sep 17 00:00:00 2001 From: j <13580441+gary02@users.noreply.github.com> Date: Mon, 26 Aug 2024 15:23:00 +0800 Subject: [PATCH] fix(pagination): fix backward pagination order --- src/common/utils/connections.ts | 12 ++++--- src/types/__test__/1/collection.test.ts | 45 ++++++++++++++++++------- 2 files changed, 39 insertions(+), 18 deletions(-) diff --git a/src/common/utils/connections.ts b/src/common/utils/connections.ts index 4a869be8c..00c6b3000 100644 --- a/src/common/utils/connections.ts +++ b/src/common/utils/connections.ts @@ -235,7 +235,7 @@ export const connectionFromQuery = async ({ .from(baseTableName) .orderBy( orderBy.column as string, - orderBy.order === 'asc' ? 'desc' : 'asc' + orderBy.order === 'asc' ? 'desc' : 'asc' // for fetching records right after cursor by `limit`, will reverse back later ) .where( orderBy.column as string, @@ -245,10 +245,12 @@ export const connectionFromQuery = async ({ .limit(last) : [] - const beforeEdges = beforeNodes.map((node) => ({ - cursor: encodeCursor(node[cursorColumn] as string), - node, - })) + const beforeEdges = beforeNodes + .map((node) => ({ + cursor: encodeCursor(node[cursorColumn] as string), + node, + })) + .reverse() // fetch after edges let afterWhereOperator = orderBy.order === 'asc' ? '>' : '<' diff --git a/src/types/__test__/1/collection.test.ts b/src/types/__test__/1/collection.test.ts index 65ed73171..4fdb2addf 100644 --- a/src/types/__test__/1/collection.test.ts +++ b/src/types/__test__/1/collection.test.ts @@ -823,7 +823,6 @@ describe('get collection articles', () => { collectionInput: { id: collectionGlobalId }, articleInput: { last: 1, - // 6 before: data2?.node.articles.pageInfo.endCursor, reversed: false, }, @@ -854,12 +853,16 @@ describe('get collection articles', () => { expect(data4?.node.articles.pageInfo.hasNextPage).toBe(true) expect(data4?.node.articles.edges[0].node.id).toBe(articleGlobalIds[0]) - // reversed by default + // backward pagination order is correct const { errors: errors5, data: data5 } = await server.executeOperation({ query: GET_COLLECTION_ARTICLES, variables: { collectionInput: { id: collectionGlobalId }, - articleInput: { first: 2 }, + articleInput: { + last: 2, + before: data2?.node.articles.pageInfo.endCursor, + reversed: false, + }, }, }) expect(errors5).toBeUndefined() @@ -867,28 +870,44 @@ describe('get collection articles', () => { expect(data5?.node.articles.edges.length).toBe(2) expect(data5?.node.articles.pageInfo.hasPreviousPage).toBe(false) expect(data5?.node.articles.pageInfo.hasNextPage).toBe(true) - expect(data5?.node.articles.edges[0].node.id).toBe(articleGlobalIds[2]) + expect(data5?.node.articles.edges[0].node.id).toBe(articleGlobalIds[0]) expect(data5?.node.articles.edges[1].node.id).toBe(articleGlobalIds[1]) - // includeAfter + // reversed by default const { errors: errors6, data: data6 } = await server.executeOperation({ + query: GET_COLLECTION_ARTICLES, + variables: { + collectionInput: { id: collectionGlobalId }, + articleInput: { first: 2 }, + }, + }) + expect(errors6).toBeUndefined() + expect(data6?.node.articles.totalCount).toBe(3) + expect(data6?.node.articles.edges.length).toBe(2) + expect(data6?.node.articles.pageInfo.hasPreviousPage).toBe(false) + expect(data6?.node.articles.pageInfo.hasNextPage).toBe(true) + expect(data6?.node.articles.edges[0].node.id).toBe(articleGlobalIds[2]) + expect(data6?.node.articles.edges[1].node.id).toBe(articleGlobalIds[1]) + + // includeAfter + const { errors: errors7, data: data7 } = await server.executeOperation({ query: GET_COLLECTION_ARTICLES, variables: { collectionInput: { id: collectionGlobalId }, articleInput: { first: 2, - after: data5?.node.articles.pageInfo.endCursor, + after: data6?.node.articles.pageInfo.endCursor, includeAfter: true, }, }, }) - expect(errors6).toBeUndefined() - expect(data6?.node.articles.totalCount).toBe(3) - expect(data6?.node.articles.edges.length).toBe(2) - expect(data6?.node.articles.pageInfo.hasPreviousPage).toBe(true) - expect(data6?.node.articles.pageInfo.hasNextPage).toBe(false) - expect(data6?.node.articles.edges[0].node.id).toBe(articleGlobalIds[1]) - expect(data6?.node.articles.edges[1].node.id).toBe(articleGlobalIds[0]) + expect(errors7).toBeUndefined() + expect(data7?.node.articles.totalCount).toBe(3) + expect(data7?.node.articles.edges.length).toBe(2) + expect(data7?.node.articles.pageInfo.hasPreviousPage).toBe(true) + expect(data7?.node.articles.pageInfo.hasNextPage).toBe(false) + expect(data7?.node.articles.edges[0].node.id).toBe(articleGlobalIds[1]) + expect(data7?.node.articles.edges[1].node.id).toBe(articleGlobalIds[0]) // return total count if first is 0 const { errors: errors0, data: data0 } = await server.executeOperation({