Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions src/HotChocolate/Data/src/Data/Sorting/Context/SortingContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq.Expressions;
using System.Reflection;
using GreenDonut.Data;
using HotChocolate.Data.Sorting.Expressions;
using HotChocolate.Language;
using HotChocolate.Language.Visitors;
using HotChocolate.Resolvers;
Expand Down Expand Up @@ -169,7 +170,7 @@ protected override ISyntaxVisitorAction Enter(

if (inputField is SortField field)
{
context.Parents.Push(CreateSelector(context.Parents.Peek(), field.Member));
context.Parents.Push(CreateSelector(context.Parents.Peek(), field));
}
else
{
Expand Down Expand Up @@ -209,17 +210,32 @@ protected override ISyntaxVisitorAction Leave(
return base.Leave(node, context);
}

private static Expression? CreateSelector(Expression? parent, MemberInfo? member)
private static Expression? CreateSelector(Expression? parent, SortField field)
{
if (parent is null || member is null)
if (parent is null)
{
return null;
}

return member switch
if (field.Metadata is ExpressionSortMetadata { Expression: LambdaExpression expression })
{
if (expression.Parameters.Count != 1
|| expression.Parameters[0].Type != parent.Type)
{
throw ThrowHelper.QueryableSorting_ExpressionParameterInvalid(parent.Type, field);
}

return QueryableDefaultSortFieldHandler.ReplaceVariableExpressionVisitor.ReplaceParameter(
expression.Body,
expression.Parameters[0],
parent);
}

return field.Member switch
{
PropertyInfo property => Expression.Property(parent, property),
FieldInfo field => Expression.Field(parent, field),
FieldInfo member => Expression.Field(parent, member),
MethodInfo method => Expression.Call(parent, method),
_ => null
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public override bool TryHandleLeave(

public static QueryableDefaultSortFieldHandler Create(SortProviderContext _) => new();

private sealed class ReplaceVariableExpressionVisitor : ExpressionVisitor
internal sealed class ReplaceVariableExpressionVisitor : ExpressionVisitor
{
private readonly Expression _replacement;
private readonly ParameterExpression _parameter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using GreenDonut.Data;
using System.Collections.Immutable;
using HotChocolate.Data.Sorting.Expressions;
using HotChocolate.Execution;
Expand Down Expand Up @@ -514,6 +515,197 @@ public async Task AsSortDefinition_Should_KeepEveryField_When_FieldFollowsDeeper
sortDefinition?.ToString());
}

[Fact]
public async Task AsSortDefinition_Should_IncludeExpressionField_When_FieldIsDefinedByExpression()
{
// arrange
ISortingContext? context = null;
var executor = await new ServiceCollection()
.AddGraphQL()
.AddQueryType(x => x
.Name("Query")
.Field("test")
.Type<ListType<ObjectType<Book>>>()
.UseSorting<ExpressionSortType>()
.Resolve(x =>
{
context = x.GetSortingContext();
return Array.Empty<Book>();
}))
.AddSorting()
.BuildRequestExecutorAsync(cancellationToken: TestContext.Current.CancellationToken);

const string query =
"""
{
test(order: [{ total: DESC }, { title: ASC }]) {
title
}
}
""";

await executor.ExecuteAsync(query, TestContext.Current.CancellationToken);

// act
var sortDefinition = context?.AsSortDefinition<Book>();

// assert
Assert.Equal(
"{t => (t.Pages * t.Chapters):DESC,t => t.Title:ASC}",
sortDefinition?.ToString());
}

[Fact]
public async Task AsSortDefinition_Should_SortByExpression_When_ExpressionFieldIsOnNestedType()
{
// arrange
ISortingContext? context = null;
var executor = await new ServiceCollection()
.AddGraphQL()
.AddQueryType(x => x
.Name("Query")
.Field("test")
.Type<ListType<ObjectType<Shelf>>>()
.UseSorting<ShelfExpressionSortType>()
.Resolve(x =>
{
context = x.GetSortingContext();
return Array.Empty<Shelf>();
}))
.AddSorting()
.BuildRequestExecutorAsync(cancellationToken: TestContext.Current.CancellationToken);

const string query =
"""
{
test(order: { book: { total: DESC } }) {
id
}
}
""";

await executor.ExecuteAsync(query, TestContext.Current.CancellationToken);

Shelf[] shelves =
[
new() { Id = 1, Book = new Book { Pages = 5, Chapters = 5 } },
new() { Id = 2, Book = new Book { Pages = 10, Chapters = 1 } },
new() { Id = 3, Book = new Book { Pages = 2, Chapters = 8 } }
];

// act
var sortDefinition = context?.AsSortDefinition<Shelf>();
var ordered = shelves.AsQueryable().OrderBy(sortDefinition).Select(t => t.Id).ToArray();

// assert
Assert.Equal(
"{t => (t.Book.Pages * t.Book.Chapters):DESC}",
sortDefinition?.ToString());
Assert.Equal([1, 3, 2], ordered);
}

[Fact]
public async Task AsSortDefinition_Should_SortByMethod_When_FieldIsBoundToMethod()
{
// arrange
ISortingContext? context = null;
var executor = await new ServiceCollection()
.AddGraphQL()
.AddQueryType(x => x
.Name("Query")
.Field("test")
.Type<ListType<ObjectType<Item>>>()
.UseSorting<ItemMethodSortType>()
.Resolve(x =>
{
context = x.GetSortingContext();
return Array.Empty<Item>();
}))
.AddSorting()
.BuildRequestExecutorAsync(cancellationToken: TestContext.Current.CancellationToken);

const string query =
"""
{
test(order: { total: DESC }) {
id
}
}
""";

await executor.ExecuteAsync(query, TestContext.Current.CancellationToken);

Item[] items =
[
new() { Id = 1, Name = "c", Price = 4, Quantity = 4 },
new() { Id = 2, Name = "a", Price = 5, Quantity = 5 },
new() { Id = 3, Name = "b", Price = 10, Quantity = 1 }
];

// act
var sortDefinition = context?.AsSortDefinition<Item>();
var ordered = items.AsQueryable().OrderBy(sortDefinition).Select(t => t.Id).ToArray();

// assert
Assert.Equal("{t => t.GetTotal():DESC}", sortDefinition?.ToString());
Assert.Equal([2, 1, 3], ordered);
}

public class ShelfExpressionSortType : SortInputType<Shelf>
{
protected override void Configure(ISortInputTypeDescriptor<Shelf> descriptor)
{
descriptor.BindFieldsExplicitly();
descriptor.Field(x => x.Book).Type<BookTotalSortType>();
}
}

public class BookTotalSortType : SortInputType<Book>
{
protected override void Configure(ISortInputTypeDescriptor<Book> descriptor)
{
descriptor.BindFieldsExplicitly();
descriptor.Field(x => x.Pages * x.Chapters).Name("total");
}
}

public class ItemMethodSortType : SortInputType<Item>
{
protected override void Configure(ISortInputTypeDescriptor<Item> descriptor)
{
descriptor.BindFieldsExplicitly();
descriptor
.Field("total")
.Type<DefaultSortEnumType>()
.Extend()
.Configuration
.Member = typeof(Item).GetMethod(nameof(Item.GetTotal));
}
}

public class Item
{
public int Id { get; set; }

public string? Name { get; set; }

public int Price { get; set; }

public int Quantity { get; set; }

public int GetTotal() => Price * Quantity;
}

public class ExpressionSortType : SortInputType<Book>
{
protected override void Configure(ISortInputTypeDescriptor<Book> descriptor)
{
descriptor.BindFieldsExplicitly();
descriptor.Field(x => x.Pages * x.Chapters).Name("total");
descriptor.Field(x => x.Title);
}
}

public class TestSortType : SortInputType<Book>
{
protected override void Configure(ISortInputTypeDescriptor<Book> descriptor)
Expand Down
Loading