/// <summary> /// Initializes <see cref="CollectionSegment" />. /// </summary> /// <param name="items"> /// The items that belong to this page. /// </param> /// <param name="info"> /// Additional information about this page. /// </param> /// <param name="getTotalCount"> /// A delegate to request the the total count. /// </param> public CollectionSegment( IReadOnlyCollection <T> items, CollectionSegmentInfo info, Func <CancellationToken, ValueTask <int> > getTotalCount) : base(new CollectionWrapper(items), info, getTotalCount) { Items = items; }
/// <summary> /// Initializes <see cref="CollectionSegment" />. /// </summary> /// <param name="items"> /// The items that belong to this page. /// </param> /// <param name="info"> /// Additional information about this page. /// </param> /// <param name="getTotalCount"> /// A delegate to request the the total count. /// </param> public CollectionSegment( IReadOnlyCollection <object> items, CollectionSegmentInfo info, Func <CancellationToken, ValueTask <int> > getTotalCount) { Items = items ?? throw new ArgumentNullException(nameof(items)); Info = info ?? throw new ArgumentNullException(nameof(info)); _getTotalCount = getTotalCount ?? throw new ArgumentNullException(nameof(getTotalCount)); }
protected override ValueTask <CollectionSegment> SliceAsync(IResolverContext context, object source, OffsetPagingArguments arguments) { //If Appropriate we handle the values here to ensure that no post-processing is done other than // correctly mapping the results into a GraphQL Collection Segment with appropriate Paging Details... if (source is IPreProcessedOffsetPageResults <TEntity> pagedResults) { bool includeTotalCountEnabled = this.PagingOptions.IncludeTotalCount ?? PagingDefaults.IncludeTotalCount; var graphQLParamsContext = new GraphQLParamsContext(context); //Optimized to only require TotalCount value if the query actually requested it! if (includeTotalCountEnabled && graphQLParamsContext.IsTotalCountRequested && pagedResults.TotalCount == null) { throw new InvalidOperationException($"Total Count is requested in the query, but was not provided with the results [{this.GetType().GetTypeName()}] from the resolvers pre-processing logic; TotalCount is null."); } int?totalCount = pagedResults.TotalCount; //Ensure we are null safe and return a valid empty list by default. var segmentResults = pagedResults?.ToList() ?? new List <TEntity>(); var collectionSegmentInfo = new CollectionSegmentInfo( hasNextPage: pagedResults?.HasNextPage ?? false, hasPreviousPage: pagedResults?.HasPreviousPage ?? false ); var graphQLConnection = new CollectionSegment( (IReadOnlyCollection <object>)segmentResults, collectionSegmentInfo, ct => new ValueTask <int>(totalCount ?? throw new InvalidOperationException()) ); return(new ValueTask <CollectionSegment>(graphQLConnection)); } throw new GraphQLException($"[{nameof(PreProcessedOffsetPagingHandler<TEntity>)}] cannot handle the specified data source of type [{source.GetType().Name}]."); }