Skip to content

Commit

Permalink
Add list input for sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib committed Oct 16, 2020
2 parents 548b964 + a4946fa commit 911db2a
Show file tree
Hide file tree
Showing 25 changed files with 365 additions and 19 deletions.
Expand Up @@ -56,7 +56,9 @@ public override FieldMiddleware CreateExecutor<TEntityType>(NameString argumentN
source = e.AsQueryable();
}

if (source != null && argument.Type is ISortInputType sortInput)
if (source != null &&
argument.Type is ListType lt &&
lt.ElementType is ISortInputType sortInput)
{
var visitorContext = new QueryableSortContext(
sortInput,
Expand Down
Expand Up @@ -167,6 +167,8 @@ public static class SortObjectFieldDescriptorExtensions
throw SortObjectFieldDescriptorExtensions_CannotInfer();
}
argumentType = typeof(ListType<>).MakeGenericType(argumentType);
var argumentDefinition = new ArgumentDefinition
{
Name = argumentPlaceholder,
Expand Down
Expand Up @@ -87,7 +87,7 @@ protected override ISyntaxVisitorAction Enter(IValueNode node, TContext context)
}
else
{
throw new InvalidOperationException();
return Continue;
}

return Break;
Expand All @@ -104,7 +104,7 @@ protected override ISyntaxVisitorAction Enter(IValueNode node, TContext context)
}
else
{
throw new InvalidOperationException();
return Continue;
}

return Break;
Expand Down
Expand Up @@ -51,6 +51,28 @@ public async Task Create_Boolean_OrderBy()
res2.MatchSnapshot("DESC");
}

[Fact]
public async Task Create_Boolean_OrderBy_List()
{
// arrange
IRequestExecutor tester = _cache.CreateSchema<Foo, FooSortType>(_fooEntities);

// act
IExecutionResult res1 = await tester.ExecuteAsync(
QueryRequestBuilder.New()
.SetQuery("{ root(order: [{ bar: ASC}]){ bar}}")
.Create());

IExecutionResult res2 = await tester.ExecuteAsync(
QueryRequestBuilder.New()
.SetQuery("{ root(order: [{ bar: DESC}]){ bar}}")
.Create());

// assert
res1.MatchSnapshot("ASC");
res2.MatchSnapshot("DESC");
}

[Fact]
public async Task Create_Boolean_OrderBy_Nullable()
{
Expand Down
Expand Up @@ -273,6 +273,7 @@ public async Task Create_ObjectNullableEnum_OrderBy()

res2.MatchSnapshot("13");
}

[Fact]
public async Task Create_ObjectString_OrderBy()
{
Expand Down Expand Up @@ -327,6 +328,7 @@ public async Task Create_ObjectNullableString_OrderBy()

res2.MatchSnapshot("13");
}

[Fact]
public async Task Create_ObjectBool_OrderBy()
{
Expand Down Expand Up @@ -382,6 +384,71 @@ public async Task Create_ObjectNullableBool_OrderBy()
res2.MatchSnapshot("13");
}

[Fact]
public async Task Create_ObjectString_OrderBy_TwoProperties()
{
// arrange
IRequestExecutor tester = _cache.CreateSchema<Bar, BarSortType>(_barEntities);

// act
// assert
IExecutionResult res1 = await tester.ExecuteAsync(
QueryRequestBuilder.New()
.SetQuery(
"{ root(order: { foo: { barBool: ASC, barShort: ASC }}) " +
"{ foo{ barBool barShort}}}")
.Create());

res1.MatchSnapshot("ASC");

IExecutionResult res2 = await tester.ExecuteAsync(
QueryRequestBuilder.New()
.SetQuery(
@"
{
root(order: [
{ foo: { barBool: ASC } },
{ foo: { barShort: ASC } }]) {
foo {
barBool
barShort
}
}
}
")
.Create());

res2.MatchSnapshot("ASC");

IExecutionResult res3 = await tester.ExecuteAsync(
QueryRequestBuilder.New()
.SetQuery(
"{ root(order: { foo: { barBool: DESC, barShort: DESC}}) " +
"{ foo{ barBool barShort}}}")
.Create());

res3.MatchSnapshot("DESC");

IExecutionResult res4 = await tester.ExecuteAsync(
QueryRequestBuilder.New()
.SetQuery(
@"
{
root(order: [
{ foo: { barBool: DESC } },
{ foo: { barShort: DESC } }]) {
foo {
barBool
barShort
}
}
}
")
.Create());

res4.MatchSnapshot("DESC");
}

public class Foo
{
public int Id { get; set; }
Expand Down
@@ -0,0 +1,12 @@
{
"data": {
"root": [
{
"bar": false
},
{
"bar": true
}
]
}
}
@@ -0,0 +1,12 @@
{
"data": {
"root": [
{
"bar": true
},
{
"bar": false
}
]
}
}
@@ -0,0 +1,24 @@
{
"data": {
"root": [
{
"foo": {
"barBool": false,
"barShort": 13
}
},
{
"foo": {
"barBool": true,
"barShort": 12
}
},
{
"foo": {
"barBool": true,
"barShort": 14
}
}
]
}
}
@@ -0,0 +1,24 @@
{
"data": {
"root": [
{
"foo": {
"barBool": true,
"barShort": 14
}
},
{
"foo": {
"barBool": true,
"barShort": 12
}
},
{
"foo": {
"barBool": false,
"barShort": 13
}
}
]
}
}
Expand Up @@ -5,14 +5,12 @@

namespace HotChocolate.Data.Sorting
{

public class QueryableSortVisitorBooleanTests
: IClassFixture<SchemaCache>
{
private static readonly Foo[] _fooEntities =
{
new Foo { Bar = true },
new Foo { Bar = false }
new Foo { Bar = true }, new Foo { Bar = false }
};

private static readonly FooNullable[] _fooNullableEntities =
Expand Down Expand Up @@ -52,6 +50,28 @@ public async Task Create_Boolean_OrderBy()
res2.MatchSqlSnapshot("DESC");
}

[Fact]
public async Task Create_Boolean_OrderBy_List()
{
// arrange
IRequestExecutor tester = _cache.CreateSchema<Foo, FooSortType>(_fooEntities);

// act
IExecutionResult res1 = await tester.ExecuteAsync(
QueryRequestBuilder.New()
.SetQuery("{ root(order: [{ bar: ASC}]){ bar}}")
.Create());

IExecutionResult res2 = await tester.ExecuteAsync(
QueryRequestBuilder.New()
.SetQuery("{ root(order: [{ bar: DESC}]){ bar}}")
.Create());

// assert
res1.MatchSnapshot("ASC");
res2.MatchSnapshot("DESC");
}

[Fact]
public async Task Create_Boolean_OrderBy_Nullable()
{
Expand Down
Expand Up @@ -358,7 +358,7 @@ public async Task Create_ObjectNullableBool_OrderBy()
.SetQuery(
"{ root(order: { foo: { barBool: ASC}}) " +
"{ foo{ barBool}}}")
.Create());
.Create());

IExecutionResult res2 = await tester.ExecuteAsync(
QueryRequestBuilder.New()
Expand All @@ -372,6 +372,71 @@ public async Task Create_ObjectNullableBool_OrderBy()
res2.MatchSqlSnapshot("13");
}

[Fact]
public async Task Create_ObjectString_OrderBy_TwoProperties()
{
// arrange
IRequestExecutor tester = _cache.CreateSchema<Bar, BarSortType>(_barEntities);

// act
// assert
IExecutionResult res1 = await tester.ExecuteAsync(
QueryRequestBuilder.New()
.SetQuery(
"{ root(order: { foo: { barBool: ASC, barShort: ASC }}) " +
"{ foo{ barBool barShort}}}")
.Create());

res1.MatchSnapshot("ASC");

IExecutionResult res2 = await tester.ExecuteAsync(
QueryRequestBuilder.New()
.SetQuery(
@"
{
root(order: [
{ foo: { barBool: ASC } },
{ foo: { barShort: ASC } }]) {
foo {
barBool
barShort
}
}
}
")
.Create());

res2.MatchSnapshot("ASC");

IExecutionResult res3 = await tester.ExecuteAsync(
QueryRequestBuilder.New()
.SetQuery(
"{ root(order: { foo: { barBool: DESC, barShort: DESC}}) " +
"{ foo{ barBool barShort}}}")
.Create());

res3.MatchSnapshot("DESC");

IExecutionResult res4 = await tester.ExecuteAsync(
QueryRequestBuilder.New()
.SetQuery(
@"
{
root(order: [
{ foo: { barBool: DESC } },
{ foo: { barShort: DESC } }]) {
foo {
barBool
barShort
}
}
}
")
.Create());

res4.MatchSnapshot("DESC");
}

public class Foo
{
public int Id { get; set; }
Expand Down
@@ -0,0 +1,12 @@
{
"data": {
"root": [
{
"bar": false
},
{
"bar": true
}
]
}
}
@@ -0,0 +1,12 @@
{
"data": {
"root": [
{
"bar": true
},
{
"bar": false
}
]
}
}

0 comments on commit 911db2a

Please sign in to comment.