Skip to content
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

feat(Sorting): adds nullable sort support to v10 #1419

Merged
merged 3 commits into from
Jan 27, 2020
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
67 changes: 67 additions & 0 deletions src/Core/Types.Sorting.Tests/QueryableSortVisitorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,72 @@ public void Sort_NoSortSpecified_ShouldReturnUnalteredSource()
);
}

[Fact]
public void Sort_Nullable_ShouldSortNullableProperlyAsc()
{
// arrange
var value = new ObjectValueNode(
new ObjectFieldNode("nullableInt",
new EnumValueNode(SortOperationKind.Asc)
)
);

FooSortType sortType = CreateType(new FooSortType());

IQueryable<Foo> a = new[]
{
new Foo {Bar = "b"},
new Foo {Bar = "c", NullableInt = 2},
new Foo {Bar = "a", NullableInt = 1}
}.AsQueryable();

// act
var filter = new QueryableSortVisitor(
sortType, typeof(Foo));
value.Accept(filter);
IQueryable<Foo> aFiltered = filter.Sort(a);

// assert
Assert.Collection(aFiltered,
foo => Assert.Equal("b", foo.Bar),
foo => Assert.Equal("a", foo.Bar),
foo => Assert.Equal("c", foo.Bar)
);
}

[Fact]
public void Sort_Nullable_ShouldSortNullableProperlyDesc()
{
// arrange
var value = new ObjectValueNode(
new ObjectFieldNode("nullableInt",
new EnumValueNode(SortOperationKind.Desc)
)
);

FooSortType sortType = CreateType(new FooSortType());

IQueryable<Foo> a = new[]
{
new Foo {Bar = "b"},
new Foo {Bar = "c", NullableInt = 2},
new Foo {Bar = "a", NullableInt = 1}
}.AsQueryable();

// act
var filter = new QueryableSortVisitor(
sortType, typeof(Foo));
value.Accept(filter);
IQueryable<Foo> aFiltered = filter.Sort(a);

// assert
Assert.Collection(aFiltered,
foo => Assert.Equal("c", foo.Bar),
foo => Assert.Equal("a", foo.Bar),
foo => Assert.Equal("b", foo.Bar)
);
}

public class FooSortType
: SortInputType<Foo>
{
Expand All @@ -160,6 +226,7 @@ protected override void Configure(

public class Foo
{
public int? NullableInt { get; set; }
public string Bar { get; set; }
public string Baz { get; set; }
}
Expand Down
1 change: 1 addition & 0 deletions src/Core/Types.Sorting.Tests/SortInputTypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ private class FooDirective { }

private class Foo
{
public bool? NullableBoolean { get; set; }
public string Bar { get; set; }
public string Baz { get; set; }
}
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Types.Sorting.Tests/Types.Sorting.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="Current">
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="Current">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<ChilliCurrentDirectory>$(MSBuildThisFileDirectory.TrimEnd('\').TrimEnd('/'))</ChilliCurrentDirectory>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Query {
input FooSort {
bar: SortOperationKind
baz: SortOperationKind
nullableBoolean: SortOperationKind
}

enum SortOperationKind {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Query {
input FooSort {
bar: SortOperationKind
baz: SortOperationKind
nullableBoolean: SortOperationKind
}

enum SortOperationKind {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type Query {

input FooSort {
bar: SortOperationKind
nullableBoolean: SortOperationKind
}

enum SortOperationKind {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type Query {

input FooSort {
bar: SortOperationKind
nullableBoolean: SortOperationKind
quux: SortOperationKind
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type Query {
input FooSort {
bar: SortOperationKind
baz: SortOperationKind
nullableBoolean: SortOperationKind
}

enum SortOperationKind {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Query {
input FooSort @foo {
bar: SortOperationKind
baz: SortOperationKind
nullableBoolean: SortOperationKind
}

enum SortOperationKind {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Query {
input FooSort @foo {
bar: SortOperationKind
baz: SortOperationKind
nullableBoolean: SortOperationKind
}

enum SortOperationKind {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Query {
input FooSort @foo {
bar: SortOperationKind
baz: SortOperationKind
nullableBoolean: SortOperationKind
}

enum SortOperationKind {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Query {
input FooSort @foo {
bar: SortOperationKind
baz: SortOperationKind
nullableBoolean: SortOperationKind
}

enum SortOperationKind {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Query {
input FooSort @foo {
bar: SortOperationKind
baz: SortOperationKind
nullableBoolean: SortOperationKind
}

enum SortOperationKind {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Query {
input Test {
bar: SortOperationKind
baz: SortOperationKind
nullableBoolean: SortOperationKind
}

enum SortOperationKind {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Query {
input StringFoo {
bar: SortOperationKind
baz: SortOperationKind
nullableBoolean: SortOperationKind
}

enum SortOperationKind {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Query {
input StringFoo {
bar: SortOperationKind
baz: SortOperationKind
nullableBoolean: SortOperationKind
}

enum SortOperationKind {
Expand Down
10 changes: 9 additions & 1 deletion src/Core/Types.Sorting/SortInputTypeDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,15 @@ private bool TryCreateImplicitSorting(
PropertyInfo property,
out SortFieldDefinition definition)
{
if (typeof(IComparable).IsAssignableFrom(property.PropertyType))
Type type = property.PropertyType;

if (type.IsGenericType
&& System.Nullable.GetUnderlyingType(type) is Type nullableType)
{
type = nullableType;
}

if (typeof(IComparable).IsAssignableFrom(type))
{
var field = new SortFieldDescriptor(Context, property);
definition = field.CreateDefinition();
Expand Down