Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save PascalSenn/ddd091d1299e950f1e833a2119ef3928 to your computer and use it in GitHub Desktop.
Save PascalSenn/ddd091d1299e950f1e833a2119ef3928 to your computer and use it in GitHub Desktop.
useOffsetPagingAttribute
public sealed class UseOffsetPagingAttribute : DescriptorAttribute
{
protected override void TryConfigure(
HotChocolate.Types.Descriptors.IDescriptorContext context,
IDescriptor descriptor,
ICustomAttributeProvider element)
{
if (element is MemberInfo m)
{
if (descriptor is IObjectFieldDescriptor ofd)
{
ofd.Argument("skip", x => x.Type<IntType>())
.Argument("take", x => x.Type<IntType>())
.Use(next => async context =>
{
await next(context);
IQueryable<int> source = null;
if (context.Result is IQueryable<int> q)
{
source = q;
}
else if (context.Result is IEnumerable<int> e)
{
source = e.AsQueryable();
}
if (source != null)
{
var skip = context.Argument<Optional<int>>("skip");
if (skip.HasValue)
{
source = source.Skip(skip.Value);
}
var take = context.Argument<Optional<int>>("take");
if (take.HasValue)
{
source = source.Take(take.Value);
}
context.Result = source;
}
});
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment