Created
March 3, 2020 12:09
-
-
Save PascalSenn/ddd091d1299e950f1e833a2119ef3928 to your computer and use it in GitHub Desktop.
useOffsetPagingAttribute
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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