Exemple #1
0
    /// <summary>
    /// Applies the offset pagination algorithm to the <paramref name="query"/>.
    /// </summary>
    /// <param name="query">
    /// The query on which the the offset pagination algorithm shall be applied to.
    /// </param>
    /// <param name="context">
    /// The field resolver context.
    /// </param>
    /// <param name="defaultPageSize">
    /// The default page size if no boundaries are set.
    /// </param>
    /// <param name="totalCount">
    /// The total count if already known.
    /// </param>
    /// <param name="cancellationToken">
    /// The cancellation token.
    /// </param>
    /// <typeparam name="TEntity">
    /// The entity type.
    /// </typeparam>
    /// <returns>
    /// Returns a collection segment instance that represents the result of applying the
    /// offset paging algorithm to the provided <paramref name="query"/>.
    /// </returns>
    public static ValueTask <CollectionSegment <TEntity> > ApplyOffsetPaginationAsync <TEntity>(
        this IQueryable <TEntity> query,
        IResolverContext context,
        int?defaultPageSize = null,
        int?totalCount      = null,
        CancellationToken cancellationToken = default)
    {
        if (query is null)
        {
            throw new ArgumentNullException(nameof(query));
        }

        if (context is null)
        {
            throw new ArgumentNullException(nameof(context));
        }

        var skip = context.ArgumentValue <int?>(OffsetPagingArgumentNames.Skip);
        var take = context.ArgumentValue <int?>(OffsetPagingArgumentNames.Take) ??
                   defaultPageSize;

        var arguments = new OffsetPagingArguments(skip, take);

        if (totalCount is null && context.IsTotalCountSelected())
        {
            totalCount = query.Count();
        }

        return(QueryableOffsetPagination <TEntity> .Instance.ApplyPaginationAsync(
                   query,
                   arguments,
                   totalCount,
                   cancellationToken));
    }
Exemple #2
0
    /// <summary>
    /// Applies the cursor pagination algorithm to the <paramref name="query"/>.
    /// </summary>
    /// <param name="query">
    /// The query on which the the cursor pagination algorithm shall be applied to.
    /// </param>
    /// <param name="context">
    /// The field resolver context.
    /// </param>
    /// <param name="defaultPageSize">
    /// The default page size if no boundaries are set.
    /// </param>
    /// <param name="totalCount">
    /// The total count if already known.
    /// </param>
    /// <param name="cancellationToken">
    /// The cancellation token.
    /// </param>
    /// <typeparam name="TEntity">
    /// The entity type.
    /// </typeparam>
    /// <returns>
    /// Returns a connection instance that represents the result of applying the
    /// cursor paging algorithm to the provided <paramref name="query"/>.
    /// </returns>
    public static ValueTask <Connection <TEntity> > ApplyCursorPaginationAsync <TEntity>(
        this IQueryable <TEntity> query,
        IResolverContext context,
        int?defaultPageSize = null,
        int?totalCount      = null,
        CancellationToken cancellationToken = default)
    {
        if (query is null)
        {
            throw new ArgumentNullException(nameof(query));
        }

        if (context is null)
        {
            throw new ArgumentNullException(nameof(context));
        }

        var first = context.ArgumentValue <int?>(CursorPagingArgumentNames.First);
        var last  = context.ArgumentValue <int?>(CursorPagingArgumentNames.Last);

        if (totalCount is null && context.IsTotalCountSelected())
        {
            totalCount = query.Count();
        }

        if (first is null && last is null)
        {
            first = defaultPageSize;
        }

        var arguments = new CursorPagingArguments(
            first,
            last,
            context.ArgumentValue <string?>(CursorPagingArgumentNames.After),
            context.ArgumentValue <string?>(CursorPagingArgumentNames.Before));

        return(QueryableCursorPagination <TEntity> .Instance.ApplyPaginationAsync(
                   query,
                   arguments,
                   totalCount,
                   cancellationToken));
    }