Skip to content

Cursor pagination with enum sorting #8173

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
marcin-janiak opened this issue Mar 25, 2025 · 0 comments
Open

Cursor pagination with enum sorting #8173

marcin-janiak opened this issue Mar 25, 2025 · 0 comments

Comments

@marcin-janiak
Copy link

Product

Green Donut

Version

15.1.1

Link to minimal reproduction

https://github.com/marcin-janiak/cursor-based-pagination-with-enum-sorting/tree/main

Steps to reproduce

Clone, run and observe console

What is expected?

Cursor pagination with EF (using AddDbContextCursorPagingProvider) and enum-based sorting should be supported, as enums are considered straightforward types.

What is actually happening?

ICursorKeySerializer and ExpressionHelpers appear to be incompatible with enums (IComparable), while they are compatible with types that support IComparable<T>.

The errors do not appear in the initial query, but rather in the subsequent query when the "after" parameter is applied.

Relevant log output

Expression of type Host.SomeEnum cannot be used for parameter of type System.Object of method Int32 CompareTo(System.Object) (Parameter arg0).
 at System.Dynamic.Utils.ExpressionUtils.ValidateOneArgument(MethodBase method, ExpressionType nodeKind, Expression arguments, ParameterInfo pi, String methodParamName, String argumentParamName, Int32 index)\r\n
at System.Linq.Expressions.Expression.Call(Expression instance, MethodInfo method, Expression arg0)\r\n
at System.Linq.Expressions.Expression.Call(Expression instance, MethodInfo method, IEnumerable`1 arguments)\r\n
at GreenDonut.Data.Expressions.ExpressionHelpers.BuildWhereExpression[T](ReadOnlySpan`1 keys, Cursor cursor, Boolean forward)\r\n
at HotChocolate.Data.Pagination.EfQueryableCursorPagingHandler`1.SliceAsync(IResolverContext context, IQueryableExecutable`1 executable, CursorPagingArguments arguments)\r\n
at HotChocolate.Types.Pagination.CursorPagingHandler.HotChocolate.Types.Pagination.IPagingHandler.SliceAsync(IResolverContext context, Object source)\r\n
at HotChocolate.Types.Pagination.PagingMiddleware.InvokeAsync(IMiddlewareContext context)\r\n
at HotChocolate.Execution.Processing.Tasks.ResolverTask.ExecuteResolverPipelineAsync(CancellationToken cancellationToken)\r\n
at HotChocolate.Execution.Processing.Tasks.ResolverTask.TryExecuteAsync(CancellationToken cancellationToken)"

"The LINQ expression 'DbSet<User>()
    .OrderBy(u => u.SkillType)
    .Where(u => u.SkillType.CompareTo((object)__value_0) > 0)' could not be translated. 

Additional information: Translation of method 'System.Enum.CompareTo' failed. 

Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'."

Additional context

Please refer to the repro README for a proof-of-concept solution. Is this a valid direction to proceed? Additionally, could this behavior be considered a bug, or is it an intentional design decision?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants