Skip to content

QueryContext<T> drops sort fields that come after a nested object field #10489

Description

@rutgersc

Product

Hot Chocolate

Version

16.5.1

Link to minimal reproduction

https://github.com/rutgersc/hotchocolate-querycontext-repro/blob/sort-drop-16/SortDropAfterNestedObjectTests.cs

Steps to reproduce

See repro link above.

public class Item { public int Id; public string Name; public int Position; public Category Category; }
public class Category { public string Name; }

[UseSorting]
public List<Item> GetItems(QueryContext<Item> context)
    => items.AsQueryable().With(context).ToList();
items(order: { category: { name: ASC }, position: ASC }) {
    id
}

QueryContext<Item>.Sorting contains only category.name. position is missing, so rows with the same category name come back unsorted.

Without QueryContext, with [UseSorting] applying the same order argument to a returned IQueryable<>, both fields are sorted:

[UseSorting]
public IQueryable<Item> GetItems()
    => items.AsQueryable();

This querycontext test fails: https://github.com/rutgersc/hotchocolate-querycontext-repro/blob/sort-drop-16/SortDropAfterNestedObjectTests.cs#L84

position is also dropped:

  • as a second list item: [{ category: { name: ASC } }, { position: ASC }]
  • one level down: { item: { category: { name: ASC }, position: ASC } }

It works when position comes first: { position: ASC, category: { name: ASC } }.

What is expected?

Similar to #9066 and #9111, QueryContext<>.Sorting should contain the same sort as [UseSorting] applies without QueryContext: category.name, then position.

What is actually happening?

position is dropped without an error, so the result is in the wrong order.

This is the shape of #8191. #8356 fixed the crash, but its test (author: { name: ASC, age: ASC }, title: DESC) has a unique author name per book, so it still passes when title is dropped.

The crash from #8191 also still happens when the field after the nested object has the same name as a field inside it: { category: { name: ASC }, name: ASC } (see log output).

Relevant log output

Property 'System.String Name' is not defined for type 'HotChocolateSortNullMemberRepro.Item' (Parameter 'property')
   at System.Linq.Expressions.Expression.Property(Expression expression, PropertyInfo property)
   at HotChocolate.Data.Sorting.SortingContext.SortDefinitionFormatter.CreateSelector(Expression parent, MemberInfo member)
   at HotChocolate.Data.Sorting.SortingContext.SortDefinitionFormatter.Enter(ObjectFieldNode node, Context context)
   at HotChocolate.Language.Visitors.SyntaxWalker`1.Enter(ISyntaxNode node, TContext context)
   at HotChocolate.Language.Visitors.SyntaxVisitor`1.Visit[TNode,TParent](TNode node, TParent parent, TContext context)
   at HotChocolate.Language.Visitors.SyntaxVisitor`1.VisitChildren(ObjectValueNode node, TContext context)
   at HotChocolate.Language.Visitors.SyntaxVisitor`1.VisitChildren(ISyntaxNode node, TContext context)
   at HotChocolate.Language.Visitors.SyntaxVisitor`1.Visit[TNode,TParent](TNode node, TParent parent, TContext context)
   at HotChocolate.Language.Visitors.SyntaxVisitor`1.Visit(ISyntaxNode node, TContext context)
   at HotChocolate.Data.Sorting.SortingContext.SortDefinitionFormatter.Rewrite(IValueNode node, IType type, Expression parameter)
   at HotChocolate.Data.Sorting.SortingContext.AsSortDefinition[T]()
   at HotChocolate.Data.QueryContextParameterExpressionBuilder.CreateQueryContext[T](IResolverContext context)

Additional context

No response

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

Metadata

Metadata

Assignees

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions