nestjs-query icon indicating copy to clipboard operation
nestjs-query copied to clipboard

Problem with cursor paging and sorting by a field with null values

Open abrenoch opened this issue 8 months ago • 3 comments

Describe the bug We discovered an issue when sorting by a field with null values while using the PagingStrategies.CURSOR strategy. The issue manifests itself as no additional items loading beyond the first query, but upon further investigation it seems the query used to recover the 'cursor' item actually contains a small syntax problem (that doesn't cause an error - at least in MariaDB).

Have you read the Contributing Guidelines?

yes

To Reproduce Using the following QueryOptions on a DTO:

@QueryOptions({
  pagingStrategy: PagingStrategies.CURSOR,
  enableTotalCount: true,
  maxResultsSize: 10,
})

Steps to reproduce the behavior:

  1. Apply sorting on a nullable field as so:
sorting: [
    {field: "someNullableField", direction: "ASC"}
]
  1. The first response will indicate more page available:
            "pageInfo": {
                "hasNextPage": true,
                "hasPreviousPage": false,
                "endCursor": "eyJ0eXBlIjoia2V5c2V0IiwiZmllbGRzIjpbeyJmaWVsZCI6Imdhcm1lbnRTdHlsZSIsInZhbHVlIjpudWxsfSx7ImZpZWxkIjoiaWQiLCJ2YWx1ZSI6OTY5NjM1fV19",
                "startCursor": "eyJ0eXBlIjoia2V5c2V0IiwiZmllbGRzIjpbeyJmaWVsZCI6Imdhcm1lbnRTdHlsZSIsInZhbHVlIjpudWxsfSx7ImZpZWxkIjoiaWQiLCJ2YWx1ZSI6OTY5NDQ4fV19",
                "__typename": "PageInfo"
            },
  1. But using that endCursor to get the next page of data, no items are returned in the query:
// supplied paging: {first: 10, after: $startCursor} (using endCursor from above for $startCursor)

{
    "data": {
        "getAssets": {
            "edges": [],
            "pageInfo": {
                "hasNextPage": false,
                "hasPreviousPage": true,
                "endCursor": null,
                "startCursor": null,
                "__typename": "PageInfo"
            },
            "__typename": "AssetConnection",
            "totalCount": 247941
        }
    }
}

Expected behavior The next page of queried items is returned

Screenshots NA

Desktop (please complete the following information):

  • Node Version: 20.11
  • Nestjs-query: 6.1.1

Additional context The problem appears to be that at:
https://github.com/TriPSs/nestjs-query/blob/0f599ed1b432b3c4a6575dfc5b1fc5fee9e92289/packages/query-graphql/src/types/connection/cursor/pager/strategies/keyset.pager-strategy.ts#L113 The query produces an "eq": null filter, which at least on mysql should be "is": null.

Changing that line to:

equalities.push({ [keySetField.field]: { [keySetField.value === null ? 'is' : 'eq']: keySetField.value } });

Appears to resolve the issue for mysql - but I'm not sure if this would be correct for other drivers or if some other methodology should be considered.

abrenoch avatar Jun 12 '24 19:06 abrenoch