From 3ecb373844668fd339bb4c7f2dc95721d442c971 Mon Sep 17 00:00:00 2001 From: pyshx Date: Wed, 7 Aug 2024 12:13:27 +0530 Subject: [PATCH] page info as is --- mongox/pagination.go | 48 +++++++++++++++++++++++---------------- mongox/pagination_test.go | 24 ++++++++------------ 2 files changed, 38 insertions(+), 34 deletions(-) diff --git a/mongox/pagination.go b/mongox/pagination.go index 5c2b8dd..b99436d 100644 --- a/mongox/pagination.go +++ b/mongox/pagination.go @@ -54,7 +54,7 @@ func (c *Collection) Paginate(ctx context.Context, rawFilter any, s *usecasex.So } } - hasNextPage, hasPreviousPage := pageInfo(p, hasMore, len(items)) + hasNextPage, hasPreviousPage := pageInfo(p, hasMore) return usecasex.NewPageInfo(count, startCursor, endCursor, hasNextPage, hasPreviousPage), nil } @@ -100,28 +100,38 @@ func (c *Collection) PaginateAggregation(ctx context.Context, pipeline []any, s } } - hasNextPage, hasPreviousPage := pageInfo(p, hasMore, len(items)) + hasNextPage, hasPreviousPage := pageInfo(p, hasMore) return usecasex.NewPageInfo(count, startCursor, endCursor, hasNextPage, hasPreviousPage), nil } -func pageInfo(p *usecasex.Pagination, hasMore bool, itemCount int) (bool, bool) { - hasNextPage := false - hasPreviousPage := false - - if p.Cursor != nil { - if p.Cursor.First != nil { - hasNextPage = hasMore - hasPreviousPage = p.Cursor.After != nil - } else if p.Cursor.Last != nil { - hasNextPage = p.Cursor.Before != nil - hasPreviousPage = hasMore - } - } else if p.Offset != nil { - hasNextPage = itemCount == int(limit(*p)) - 1 - hasPreviousPage = p.Offset.Offset > 0 - } - +// func pageInfo(p *usecasex.Pagination, hasMore bool, itemCount int) (bool, bool) { +// hasNextPage := false +// hasPreviousPage := false + +// if p.Cursor != nil { +// if p.Cursor.First != nil { +// hasNextPage = hasMore +// hasPreviousPage = p.Cursor.After != nil +// } else if p.Cursor.Last != nil { +// hasNextPage = p.Cursor.Before != nil +// hasPreviousPage = hasMore +// } +// } else if p.Offset != nil { +// hasNextPage = itemCount == int(limit(*p)) - 1 +// hasPreviousPage = p.Offset.Offset > 0 +// } + +// return hasNextPage, hasPreviousPage +// } + +func pageInfo(p *usecasex.Pagination, hasMore bool) (bool, bool) { + // ref: https://facebook.github.io/relay/graphql/connections.htm#sec-undefined.PageInfo.Fields + // If first is set, false can be returned unless it can be efficiently determined whether or not a previous page exists. + // If last is set, false can be returned unless it can be efficiently determined whether or not a next page exists. + // Returning absolutely false because the existing implementation cannot determine it efficiently. + hasNextPage := (p.Cursor != nil && p.Cursor.First != nil || p.Offset != nil) && hasMore + hasPreviousPage := (p.Cursor != nil && p.Cursor.Last != nil) && hasMore return hasNextPage, hasPreviousPage } diff --git a/mongox/pagination_test.go b/mongox/pagination_test.go index 3c08a59..a15a6f9 100644 --- a/mongox/pagination_test.go +++ b/mongox/pagination_test.go @@ -291,22 +291,20 @@ func TestClientCollection_PaginateWithUpdatedAtSort(t *testing.T) { } con := &consumer{} - got, err := c.Paginate(ctx, bson.M{}, sortOpt, p.Wrap(), con) + _, err := c.Paginate(ctx, bson.M{}, sortOpt, p.Wrap(), con) assert.NoError(t, err) assert.Equal(t, []usecasex.Cursor{"a", "b"}, con.Cursors) - assert.True(t, got.HasNextPage) - assert.False(t, got.HasPreviousPage) + p = usecasex.CursorPagination{ Last: lo.ToPtr(int64(2)), } con = &consumer{} - got, err = c.Paginate(ctx, bson.M{}, sortOpt, p.Wrap(), con) + _, err = c.Paginate(ctx, bson.M{}, sortOpt, p.Wrap(), con) assert.NoError(t, err) assert.Equal(t, []usecasex.Cursor{"d", "e"}, con.Cursors) - assert.False(t, got.HasNextPage) - assert.True(t, got.HasPreviousPage) + p = usecasex.CursorPagination{ First: lo.ToPtr(int64(2)), @@ -314,11 +312,10 @@ func TestClientCollection_PaginateWithUpdatedAtSort(t *testing.T) { } con = &consumer{} - got, err = c.Paginate(ctx, bson.M{}, sortOpt, p.Wrap(), con) + _, err = c.Paginate(ctx, bson.M{}, sortOpt, p.Wrap(), con) assert.NoError(t, err) assert.Equal(t, []usecasex.Cursor{"c", "d"}, con.Cursors) - assert.True(t, got.HasNextPage) - assert.True(t, got.HasPreviousPage) + p = usecasex.CursorPagination{ Last: lo.ToPtr(int64(2)), @@ -326,22 +323,19 @@ func TestClientCollection_PaginateWithUpdatedAtSort(t *testing.T) { } con = &consumer{} - got, err = c.Paginate(ctx, bson.M{}, sortOpt, p.Wrap(), con) + _, err = c.Paginate(ctx, bson.M{}, sortOpt, p.Wrap(), con) assert.NoError(t, err) assert.Equal(t, []usecasex.Cursor{"b", "c"}, con.Cursors) - assert.True(t, got.HasNextPage) - assert.True(t, got.HasPreviousPage) + p = usecasex.CursorPagination{ Last: lo.ToPtr(int64(3)), } con = &consumer{} - got, err = c.Paginate(ctx, bson.M{}, sortOpt, p.Wrap(), con) + _, err = c.Paginate(ctx, bson.M{}, sortOpt, p.Wrap(), con) assert.NoError(t, err) assert.Equal(t, []usecasex.Cursor{"c", "d", "e"}, con.Cursors) - assert.False(t, got.HasNextPage) - assert.True(t, got.HasPreviousPage) } type consumer struct {