public async Task InvokeAsync(IMiddlewareContext context) { await _next(context).ConfigureAwait(false); IValueNode sortArgument = context.Argument <IValueNode>(_contextData.ArgumentName); 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[_contextData.ArgumentName].Type is InputObjectType iot && iot is ISortInputType fit) { QueryableSortVisitor visitor; if (source is EnumerableQuery) { visitor = new QueryableSortVisitorInMemory(iot, fit.EntityType); } else { 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_ComparableAsc_ShouldSortByStringNullableObjectAsc() { // arrange var value = new ObjectValueNode( new ObjectFieldNode("bar", new ObjectValueNode( new ObjectFieldNode("baz", new EnumValueNode(SortOperationKind.Asc) ) ) )); FooSortType sortType = CreateType(new FooSortType()); IQueryable <Foo> a = new[] { new Foo { Bar = new Bar { Baz = "b" } }, new Foo { }, new Foo { Bar = new Bar { Baz = "c" } } }.AsQueryable(); // act var filter = new QueryableSortVisitorInMemory( sortType, typeof(Foo)); value.Accept(filter); ICollection <Foo> aFiltered = filter.Sort(a).ToList(); // assert Assert.Collection(aFiltered, foo => Assert.Null(foo.Bar), foo => Assert.Equal("b", foo.Bar.Baz), foo => Assert.Equal("c", foo.Bar.Baz) ); }