On my Hot Chocolate GraphQl
queries with OffSetPagination and leveraging skip
and take
for my query.
I am noticing that skip
and take
filters works as expected on BE, but I am not seeing desired results either on playground
or Postman
. This is weird because when I hit breakpoint, it has valid data.
- What am I doing wrong here?
- Is this a bug in
[UseOffsetPaging]
?
References:
Code:
Query.cs
public class ExceptionsQuery
{
[Authorize(policy: "SomePolicy")]
[UseOffsetPaging]
public async Task<List<ExceptionDto>> GetExceptionsAsync(
ExceptionFiltersDto filters,
[Service(ServiceKind.Synchronized)] IExceptionService exceptionService,
CancellationToken cancellationToken,
int skip = 0,
int take = 0)
// => await exceptionService.GetExceptions(filters, cancellationToken, skip, take);
{
var result = await exceptionService.GetExceptions(filters, cancellationToken, skip, take);
return result; // This returns valid data
}
}
service.cs
public async Task<List<ExceptionDto>> GetExceptions(ExceptionFiltersDto filters, CancellationToken cancellationToken, int skip = 0,int take = 0)
{
try
{
if (string.IsNullOrWhiteSpace(filters.Location))
{
throw new ArgumentException("Location cannot be empty or whitespace.");
}
return await _projectXSupplyChainDbManagement.ExceptionRepository
.GetExceptions(filters.Location, cancellationToken, filters.StockNumber, filters.InvoiceType, filters.SupplierName, skip, take)
.Select(exceptionEntity => _mapper.Map<ExceptionDto>(exceptionEntity))
.ToListAsync(cancellationToken: cancellationToken);
}
catch (Exception exception)
{
_logger.Error($"[{nameof(GetExceptions)}]: Error occurred - {{@exception}}", args: new object[] { exception.GetBaseException().Message});
throw;
}
}
Repo.cs
public IAsyncEnumerable<Exception> GetExceptions(string location,
CancellationToken cancellationToken,
string stockNumber = "",
string invoiceFilter = "",
string supplierName = "", int skip = 0,int take = 0)
{
var query = _dbContext.Exception.AsNoTracking()
.Where(_ => _.Location.Equals(location));
if (!string.IsNullOrWhiteSpace(stockNumber)) query = query.Where(e => e.StockNumber.Equals(stockNumber));
// return query
// .Skip(skip * take)
// .Take(take)
// .AsAsyncEnumerable();
// return query
// .Skip(take * (skip - 1))
// .Take(take)
// .AsAsyncEnumerable();
return query
.Skip(skip)
.Take(take)
.AsAsyncEnumerable();
}
Screen Shots (Non-working example):
Postman
// As you can see, here data is present but on above screenshots, you just see 2 records.
Working example: