Exemplo n.º 1
0
        public async Task InvokeAsync(IMiddlewareContext context)
        {
            await _next(context).ConfigureAwait(false);

            var pagingDetails = new PagingDetails
            {
                First  = context.ArgumentValue <int?>("first"),
                After  = context.ArgumentValue <string>("after"),
                Last   = context.ArgumentValue <int?>("last"),
                Before = context.ArgumentValue <string>("before"),
            };

            IQueryable <T> source = context.Result switch
            {
                IQueryable <T> q => q,
                IEnumerable <T> e => e.AsQueryable(),
                _ => null
            };

            if (source != null)
            {
                IConnectionResolver connectionResolver = _createConnectionResolver(
                    source, pagingDetails);

                context.Result = await connectionResolver
                                 .ResolveAsync(context.RequestAborted)
                                 .ConfigureAwait(false);
            }
        }
        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
            });

            Connection <string> 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);
        }
        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
            });

            Connection <string> 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));
            });

            Assert.True(
                connection.PageInfo.HasPreviousPage,
                "HasPreviousPage");

            Assert.True(
                connection.PageInfo.HasNextPage,
                "HasNextPage");
        }
Exemplo n.º 4
0
        private static QueryablePagingDetails DeserializePagingDetails(
            PagingDetails pagingDetails)
        {
            Dictionary <string, object> afterProperties =
                TryDeserializeCursor(pagingDetails.After);
            Dictionary <string, object> beforeProperties =
                TryDeserializeCursor(pagingDetails.Before);

            return(new QueryablePagingDetails
            {
                After = GetPositionFromCurser(afterProperties),
                Before = GetPositionFromCurser(beforeProperties),
                TotalCount = GetTotalCountFromCursor(afterProperties)
                             ?? GetTotalCountFromCursor(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
            Connection <string> connection = await connectionFactory.ResolveAsync(
                CancellationToken.None);

            // assert
            Assert.False(connection.PageInfo.HasPreviousPage);
        }
Exemplo n.º 6
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>();
        }
        public async Task InvokeAsync(IMiddlewareContext context)
        {
            await _next(context).ConfigureAwait(false);

            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)
            {
                IConnectionResolver connectionResolver = _createConnectionResolver(
                    source, pagingDetails);

                context.Result = await connectionResolver
                                 .ResolveAsync(context.RequestAborted)
                                 .ConfigureAwait(false);
            }
        }
Exemplo n.º 8
0
        public async Task HasNextPage_True()
        {
            // arrange
            var list = new List <string> {
                "a", "b", "c", "d", "e", "f", "g",
            };

            var pagingDetails = new PagingDetails
            {
                First = 5
            };

            var connectionFactory = new QueryableConnectionResolver <string>(
                list.AsQueryable(), pagingDetails);

            // act
            var connection = await connectionFactory.ResolveAsync(
                CancellationToken.None);

            // assert
            Assert.True(connection.PageInfo.HasNextPage);
        }
 private static IConnectionResolver CreateConnectionResolver(
     IQueryable <T> source, PagingDetails pagingDetails)
 {
     return(new QueryableConnectionResolver <T>(
                source, pagingDetails));
 }