Skip to content

Commit

Permalink
#23 updated extension to include offset paging extension
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian-Webster committed Jan 16, 2024
1 parent 064f719 commit d976944
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Example/DataAccess.Example.Data/Queries/BookQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@ public async Task<Connection<Book>> GetPagedBooks([Service] IBookRepository repo
{
return await repository.GetPagedBooksForGraphQuery(context, token);
}

public async Task<CollectionSegment<Book>> GetOffsetPagedBooks([Service] IBookRepository repository, IResolverContext context, CancellationToken token)
{
return await repository.GetOffsetPagedBooksForGraphQuery(context, token);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,9 @@ public async Task<Connection<Book>> GetPagedBooksForGraphQuery(IResolverContext
{
return await _bookRepo.GetPagedQueryItems(context, token);
}

public async Task<CollectionSegment<Book>> GetOffsetPagedBooksForGraphQuery(IResolverContext context, CancellationToken token)
{
return await _bookRepo.GetOffsetPagedQueryItems(context, token);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ public interface IBookRepository
Task<IEnumerable<Book>?> GetBooksForGraphQuery(IResolverContext context, CancellationToken token);

Task<Connection<Book>> GetPagedBooksForGraphQuery(IResolverContext context, CancellationToken token);

Task<CollectionSegment<Book>> GetOffsetPagedBooksForGraphQuery(IResolverContext context, CancellationToken token);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<Description>Extension to the DataAccess package adding GraphQL functionality via the HotChocolate library</Description>
<PackageProjectUrl>https://github.com/Ian-Webster/DataAccess/Extensions/DataAccess.Repository.HotChocolate</PackageProjectUrl>
<RepositoryUrl>https://github.com/Ian-Webster/DataAccess</RepositoryUrl>
<VersionPrefix>1.0.0</VersionPrefix>
<VersionPrefix>1.1.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>

Expand Down
26 changes: 26 additions & 0 deletions Extensions/DataAccess.Repository.HotChocolate/QueryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,30 @@ public static async Task<Connection<TEntity>> GetPagedQueryItems<TEntity>(this I
.Sort(context)
.ApplyCursorPaginationAsync(context, cancellationToken:token);
}

/// <summary>
/// Returns a collection of TEntity, filtered, sorted, offset paged and projected using extension methods from https://chillicream.com/docs/hotchocolate/v13/api-reference/extending-filtering
/// </summary>
/// <remarks>
/// Variation of cursor paging that uses skip/take instead of cursor data, see https://chillicream.com/docs/hotchocolate/v13/fetching-data/pagination/#offset-pagination
/// </remarks>
/// <typeparam name="TEntity">entity type for the repository</typeparam>
/// <param name="repository">IRepository</param>
/// <param name="context">HotChocolate filter context, this is used by the IQueryable extensions to apply filtering, sorting, pagination and projection</param>
/// <param name="token"></param>
/// <returns></returns>
public static async Task<CollectionSegment<TEntity>> GetOffsetPagedQueryItems<TEntity>(this IRepository<TEntity> repository,
IResolverContext context, CancellationToken token) where TEntity : class
{
var take = context.ArgumentValue<int>("take");
var skip = context.ArgumentValue<int>("skip");

return await repository.DbSet
.AsNoTracking()
.AsQueryable()
.Filter(context)
.Project(context)
.Sort(context)
.ApplyOffsetPaginationAsync(skip, take, token);
}
}

0 comments on commit d976944

Please sign in to comment.