public void Sort_NoSortSpecified_ShouldReturnUnalteredSource()
        {
            // arrange
            var value = new ObjectValueNode();

            FooSortType sortType = CreateType(new FooSortType());

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

            // act
            var filter = new QueryableSortVisitor(
                sortType, typeof(Foo));

            value.Accept(filter);
            IQueryable <Foo> aFiltered = filter.Sort(a);

            // assert
            Assert.Same(a, aFiltered);
            Assert.Collection(aFiltered,
                              foo => Assert.Equal("b", foo.Bar),
                              foo => Assert.Equal("a", foo.Bar),
                              foo => Assert.Equal("c", foo.Bar)
                              );
        }
        public void Sort_ComparableDesc_ShouldSortByStringAsc()
        {
            // arrange
            var value = new ObjectValueNode(
                new ObjectFieldNode("bar",
                                    new EnumValueNode(SortOperationKind.Desc))
                );

            FooSortType sortType = CreateType(new FooSortType());

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

            // act
            var filter = new QueryableSortVisitor(
                sortType, typeof(Foo));

            value.Accept(filter);
            ICollection <Foo> aFiltered = filter.Sort(a).ToList();

            // assert
            Assert.Collection(aFiltered,
                              foo => Assert.Equal("c", foo.Bar),
                              foo => Assert.Equal("b", foo.Bar),
                              foo => Assert.Equal("a", foo.Bar)
                              );
        }
        public async Task InvokeAsync(IMiddlewareContext context)
        {
            await _next(context).ConfigureAwait(false);

            IValueNode sortArgument = context.Argument <IValueNode>(
                SortObjectFieldDescriptorExtensions.OrderByArgumentName);

            if (sortArgument is null || sortArgument is NullValueNode)
            {
                return;
            }

            IQueryable <T> source = null;

            if (context.Result is IQueryable <T> q)
            {
                source = q;
            }
            else if (context.Result is IEnumerable <T> e)
            {
                source = e.AsQueryable();
            }

            if (context.Result is PageableData <T> p)
            {
                source = p.Source;
            }
            else
            {
                p = null;
            }

            if (source != null &&
                context.Field
                .Arguments[SortObjectFieldDescriptorExtensions.OrderByArgumentName]
                .Type is InputObjectType iot &&
                iot is ISortInputType fit)
            {
                var visitor = new QueryableSortVisitor(
                    iot,
                    fit.EntityType);
                sortArgument.Accept(visitor);

                source         = visitor.Sort(source);
                context.Result = p is null
                    ? (object)source
                    : new PageableData <T>(source, p.Properties);
            }
        }
        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)
                              );
        }