public async Task TakeLast() { // arrange var list = new List <string> { "a", "b", "c", "d", "e", "f", "g", }; var pagingDetails = new PagingDetails { Last = 2 }; var connectionFactory = new QueryableConnectionResolver <string>( list.AsQueryable(), pagingDetails); // act var connection = await connectionFactory.ResolveAsync( CancellationToken.None); // assert Assert.Collection(connection.Edges, t => { Assert.Equal("f", t.Node); Assert.Equal(5, GetPositionFromCursor(t.Cursor)); }, t => { Assert.Equal("g", t.Node); Assert.Equal(6, GetPositionFromCursor(t.Cursor)); }); }
public async Task HasPrevious_True() { // arrange var list = new List <string> { "a", "b", "c", "d", "e", "f", "g", }; var connectionFactory = new QueryableConnectionResolver <string>( list.AsQueryable(), new PagingDetails { First = 1 }); var connection = await connectionFactory.ResolveAsync( CancellationToken.None); var pagingDetails = new PagingDetails { After = connection.PageInfo.StartCursor, First = 2 }; connectionFactory = new QueryableConnectionResolver <string>( list.AsQueryable(), pagingDetails); // act connection = await connectionFactory.ResolveAsync( CancellationToken.None); // assert Assert.True(connection.PageInfo.HasPreviousPage); }
private static QueryablePagingDetails DeserializePagingDetails( PagingDetails pagingDetails) { Dictionary <string, object> afterProperties = DeserializeCursor(pagingDetails.After); Dictionary <string, object> beforeProperties = DeserializeCursor(pagingDetails.Before); return(new QueryablePagingDetails { After = GetPositionFromCurser(afterProperties), Before = GetPositionFromCurser(beforeProperties), TotalCount = GetPositionFromCurser(afterProperties) ?? GetPositionFromCurser(beforeProperties), First = pagingDetails.First, Last = pagingDetails.Last }); }
public async Task HasPrevious_False() { // arrange var list = new List <string> { "a", "b", "c", "d", "e", "f", "g", }; var pagingDetails = new PagingDetails(); var connectionFactory = new QueryableConnectionResolver <string>( list.AsQueryable(), pagingDetails); // act var connection = await connectionFactory.ResolveAsync( CancellationToken.None); // assert Assert.False(connection.PageInfo.HasPreviousPage); }
public QueryableConnectionResolver( IQueryable <T> source, PagingDetails pagingDetails) { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (pagingDetails == null) { throw new ArgumentNullException(nameof(pagingDetails)); } _source = source; _pageDetails = DeserializePagingDetails(pagingDetails); _properties = pagingDetails.Properties ?? new Dictionary <string, object>(); }
public async Task TakeFirstAfter() { // arrange var list = new List <string> { "a", "b", "c", "d", "e", "f", "g", }; var connectionFactory = new QueryableConnectionResolver <string>( list.AsQueryable(), new PagingDetails { First = 1 }); var connection = await connectionFactory.ResolveAsync( CancellationToken.None); var pagingDetails = new PagingDetails { After = connection.PageInfo.StartCursor, First = 2 }; connectionFactory = new QueryableConnectionResolver <string>( list.AsQueryable(), pagingDetails); // act connection = await connectionFactory.ResolveAsync( CancellationToken.None); // assert Assert.Collection(connection.Edges, t => { Assert.Equal("b", t.Node); Assert.Equal(1, GetPositionFromCursor(t.Cursor)); }, t => { Assert.Equal("c", t.Node); Assert.Equal(2, GetPositionFromCursor(t.Cursor)); }); }
public async Task InvokeAsync(IMiddlewareContext context) { await _next(context); var pagingDetails = new PagingDetails { First = context.Argument <int?>("first"), After = context.Argument <string>("after"), Last = context.Argument <int?>("last"), Before = context.Argument <string>("before"), }; IQueryable <T> source = null; if (context.Result is PageableData <T> p) { source = p.Source; pagingDetails.Properties = p.Properties; } if (context.Result is IQueryable <T> q) { source = q; } if (context.Result is IEnumerable <T> e) { source = e.AsQueryable(); } if (source != null) { var connectionResolver = _createConnectionResolver( source, pagingDetails); context.Result = await connectionResolver.ResolveAsync( context.RequestAborted); } }
private static IConnectionResolver CreateConnectionResolver( IQueryable <T> source, PagingDetails pagingDetails) { return(new QueryableConnectionResolver <T>( source, pagingDetails)); }