示例#1
0
        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));
            });
        }
示例#2
0
        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);
        }
示例#3
0
        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
            });
        }
示例#4
0
        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);
        }
示例#5
0
        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>();
        }
示例#6
0
        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));
            });
        }
示例#7
0
        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);
            }
        }
示例#8
0
 private static IConnectionResolver CreateConnectionResolver(
     IQueryable <T> source, PagingDetails pagingDetails)
 {
     return(new QueryableConnectionResolver <T>(
                source, pagingDetails));
 }