diff --git a/src/GreenDonut/src/GreenDonut.Data.Primitives/SortBy.cs b/src/GreenDonut/src/GreenDonut.Data.Primitives/SortBy.cs index cfa3982ec96..ac091dd6cdf 100644 --- a/src/GreenDonut/src/GreenDonut.Data.Primitives/SortBy.cs +++ b/src/GreenDonut/src/GreenDonut.Data.Primitives/SortBy.cs @@ -81,8 +81,29 @@ public IOrderedQueryable ApplyThenBy(IOrderedQueryable queryab } } +/// +/// Provides factory methods for creating sort operations. +/// +/// +/// The entity type associated with the sort operation. +/// public static class SortBy { + /// + /// Creates a sort operation that sorts in ascending order. + /// + /// + /// The field on which the sort operation is applied. + /// + /// + /// The type of the field on which the sort operation is applied. + /// + /// + /// A sort operation that sorts in ascending order. + /// + /// + /// is null. + /// public static SortBy Ascending( Expression> keySelector) { @@ -94,6 +115,21 @@ public static SortBy Ascending( return new SortBy(keySelector, true); } + /// + /// Creates a sort operation that sorts in descending order. + /// + /// + /// The field on which the sort operation is applied. + /// + /// + /// The type of the field on which the sort operation is applied. + /// + /// + /// A sort operation that sorts in descending order. + /// + /// + /// is null. + /// public static SortBy Descending( Expression> keySelector) { diff --git a/src/GreenDonut/src/GreenDonut.Data.Primitives/SortDefinition.cs b/src/GreenDonut/src/GreenDonut.Data.Primitives/SortDefinition.cs index 4a061782811..de480f28573 100644 --- a/src/GreenDonut/src/GreenDonut.Data.Primitives/SortDefinition.cs +++ b/src/GreenDonut/src/GreenDonut.Data.Primitives/SortDefinition.cs @@ -1,4 +1,5 @@ using System.Collections.Immutable; +using System.Linq.Expressions; using System.Text; namespace GreenDonut.Data; @@ -47,6 +48,60 @@ public SortDefinition(IEnumerable> operations) public void Deconstruct(out ImmutableArray> operations) => operations = Operations; + /// + /// Adds a sort operation to the definition. + /// + /// + /// The field on which the sort operation is applied. + /// + /// + /// The type of the field on which the sort operation is applied. + /// + /// + /// The updated sort definition. + /// + public SortDefinition AddAscending( + Expression> keySelector) + { + if (keySelector == null) + { + throw new ArgumentNullException(nameof(keySelector)); + } + + var operations = Operations.Add(SortBy.Ascending(keySelector)); + return new SortDefinition(operations); + } + + /// + /// Adds a descending sort operation to the definition. + /// + /// + /// The field on which the sort operation is applied. + /// + /// + /// The type of the field on which the sort operation is applied. + /// + /// + /// The updated sort definition. + /// + public SortDefinition AddDescending( + Expression> keySelector) + { + if (keySelector == null) + { + throw new ArgumentNullException(nameof(keySelector)); + } + + var operations = Operations.Add(SortBy.Ascending(keySelector)); + return new SortDefinition(operations); + } + + /// + /// Returns a string representation of the sort definition. + /// + /// + /// A string representation of the sort definition. + /// public override string ToString() { if (Operations.Length == 0) diff --git a/src/GreenDonut/src/GreenDonut.Data/Extensions/GreenDonutPaginationBatchingDataLoaderExtensions.cs b/src/GreenDonut/src/GreenDonut.Data/Extensions/GreenDonutPaginationBatchingDataLoaderExtensions.cs index ad0da1679a1..697a4bfd6d6 100644 --- a/src/GreenDonut/src/GreenDonut.Data/Extensions/GreenDonutPaginationBatchingDataLoaderExtensions.cs +++ b/src/GreenDonut/src/GreenDonut.Data/Extensions/GreenDonutPaginationBatchingDataLoaderExtensions.cs @@ -38,19 +38,19 @@ public static IDataLoader> WithPagingArguments( this IDataLoader> dataLoader, PagingArguments pagingArguments) where TKey : notnull - => With(dataLoader, pagingArguments); + => WithInternal(dataLoader, pagingArguments, null); /// /// Branches a DataLoader with the provided . /// /// - /// The DataLoader that shall be branched. + /// The DataLoader that shall be branched. /// /// - /// The paging arguments that shall exist as state in the branched DataLoader. + /// The paging arguments that shall exist as state in the branched DataLoader. /// /// - /// The query context that shall exist as state in the branched DataLoader. + /// The query context that shall exist as state in the branched DataLoader. /// /// /// The key type of the DataLoader. @@ -64,11 +64,17 @@ public static IDataLoader> WithPagingArguments( /// /// Throws if the is null. /// - public static IDataLoader> With( - this IDataLoader> dataLoader, + public static IDataLoader> With(this IDataLoader> dataLoader, PagingArguments pagingArguments, QueryContext? context = null) where TKey : notnull + => WithInternal(dataLoader, pagingArguments, context); + + private static IDataLoader> WithInternal( + this IDataLoader> dataLoader, + PagingArguments pagingArguments, + QueryContext? context) + where TKey : notnull { if (dataLoader is null) { @@ -160,7 +166,8 @@ public static IDataLoader> Select( var branchKey = selector.ComputeHash(); var state = new QueryState(DataLoaderStateKeys.Selector, new DefaultSelectorBuilder(selector)); - return (IQueryDataLoader>)dataLoader.Branch(branchKey, DataLoaderStateHelper.CreateBranch, state); + return (IQueryDataLoader>)dataLoader.Branch(branchKey, DataLoaderStateHelper.CreateBranch, + state); } /// @@ -200,11 +207,34 @@ public static IDataLoader> Where( } var branchKey = predicate.ComputeHash(); - var state = new QueryState(DataLoaderStateKeys.Predicate, GetOrCreateBuilder(dataLoader.ContextData, predicate)); - return (IQueryDataLoader>)dataLoader.Branch(branchKey, DataLoaderStateHelper.CreateBranch, state); + var state = new QueryState(DataLoaderStateKeys.Predicate, + GetOrCreateBuilder(dataLoader.ContextData, predicate)); + return (IQueryDataLoader>)dataLoader.Branch(branchKey, DataLoaderStateHelper.CreateBranch, + state); } - public static IDataLoader> Order( + /// + /// Adds a sorting definition as state to the DataLoader. + /// + /// + /// The DataLoader. + /// + /// + /// The sorting definition that shall be added as state to the DataLoader. + /// + /// + /// The key type of the DataLoader. + /// + /// + /// The value type of the DataLoader. + /// + /// + /// Returns the DataLoader with the added projection. + /// + /// + /// Throws if the is null. + /// + public static IDataLoader> OrderBy( this IDataLoader> dataLoader, SortDefinition? sortDefinition) where TKey : notnull @@ -221,10 +251,11 @@ public static IDataLoader> Order( var branchKey = sortDefinition.ComputeHash(); var state = new QueryState(DataLoaderStateKeys.Sorting, sortDefinition); - return (IQueryDataLoader>)dataLoader.Branch(branchKey, DataLoaderStateHelper.CreateBranch, state); + return (IQueryDataLoader>)dataLoader.Branch(branchKey, DataLoaderStateHelper.CreateBranch, + state); } - private static string ComputeHash(this PagingArguments arguments, QueryContext? context = null) + private static string ComputeHash(this PagingArguments arguments, QueryContext? context) { var hasher = ExpressionHasherPool.Shared.Get(); diff --git a/src/GreenDonut/src/GreenDonut.Data/Extensions/GreenDonutQueryableExtensions.cs b/src/GreenDonut/src/GreenDonut.Data/Extensions/GreenDonutQueryableExtensions.cs index 3fc93a6229a..66af90a33c9 100644 --- a/src/GreenDonut/src/GreenDonut.Data/Extensions/GreenDonutQueryableExtensions.cs +++ b/src/GreenDonut/src/GreenDonut.Data/Extensions/GreenDonutQueryableExtensions.cs @@ -321,7 +321,7 @@ public static IOrderedQueryable ThenBy(this IOrderedQueryable queryable /// /// Throws if is null or if is null. /// - public static IQueryable Apply( + public static IQueryable With( this IQueryable queryable, QueryContext? queryContext, Func, SortDefinition>? modifySortDefinition = null) diff --git a/src/GreenDonut/src/GreenDonut/Result.cs b/src/GreenDonut/src/GreenDonut/Result.cs index f23205367cc..2a2e7407c6f 100644 --- a/src/GreenDonut/src/GreenDonut/Result.cs +++ b/src/GreenDonut/src/GreenDonut/Result.cs @@ -108,4 +108,6 @@ public static implicit operator TValue(Result result) => result.Value; } +#pragma warning disable RCS1194 public class KeyNotFoundException(string message) : Exception(message); +#pragma warning restore RCS1194 diff --git a/src/HotChocolate/Data/src/Data/Filters/Extensions/HotChocolateExecutionDataLoaderExtensions.cs b/src/HotChocolate/Data/src/Data/Filters/Extensions/HotChocolateExecutionDataLoaderExtensions.cs index 4266942e6d4..351449381c6 100644 --- a/src/HotChocolate/Data/src/Data/Filters/Extensions/HotChocolateExecutionDataLoaderExtensions.cs +++ b/src/HotChocolate/Data/src/Data/Filters/Extensions/HotChocolateExecutionDataLoaderExtensions.cs @@ -150,6 +150,6 @@ public static IDataLoader> Order( where TKey : notnull { var definition = context.AsSortDefinition(); - return dataLoader.Order(definition); + return dataLoader.OrderBy(definition); } } diff --git a/src/HotChocolate/Data/test/Data.Tests/IntegrationTests.cs b/src/HotChocolate/Data/test/Data.Tests/IntegrationTests.cs index f238537112d..54e2414b7ef 100644 --- a/src/HotChocolate/Data/test/Data.Tests/IntegrationTests.cs +++ b/src/HotChocolate/Data/test/Data.Tests/IntegrationTests.cs @@ -1196,7 +1196,7 @@ public IQueryable GetAuthorsData(QueryContext context) Books = new List() }, }.AsQueryable() - .Apply(context); + .With(context); [UseSorting] public IQueryable GetAuthorsData2(QueryContext context) @@ -1221,6 +1221,6 @@ public IQueryable GetAuthorsData2(QueryContext context) Books = new List() } }.AsQueryable() - .Apply(context, t => t with { Operations = t.Operations.Add(SortBy.Ascending(t => t.Id)) }); + .With(context, t => t with { Operations = t.Operations.Add(SortBy.Ascending(t => t.Id)) }); } }