Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply limit to IQueryable in FirstOrDefault/SingleOrDefaultMiddleware #6833

Merged
merged 5 commits into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ public async Task InvokeAsync(IMiddlewareContext context)
{
case IAsyncEnumerable<T> ae:
{
// Apply limit.
if (ae is IQueryable<T> q)
{
q = q.Take(1);

ae = (IAsyncEnumerable<T>)q;
}

await using var enumerator =
ae.GetAsyncEnumerator(context.RequestAborted);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ public async Task InvokeAsync(IMiddlewareContext context)
{
case IAsyncEnumerable<T> ae:
{
// Apply limit.
if (ae is IQueryable<T> q)
{
q = q.Take(2);

ae = (IAsyncEnumerable<T>)q;
}

await using var enumerator = ae.GetAsyncEnumerator(context.RequestAborted);

if (await enumerator.MoveNextAsync().ConfigureAwait(false))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public async Task ExecuteAsync_Should_ReturnAllItems_When_ToListAsync()
x => x
.Name("Query")
.Field("executable")
.Resolve(_authors.AsDbContextExecutable()))
.Resolve(_authors))
.BuildRequestExecutorAsync();

// act
Expand Down Expand Up @@ -63,7 +63,7 @@ public async Task ExecuteAsync_Should_OnlyOneItem_When_SingleOrDefault()
.Name("Query")
.Field("executable")
.Type<ObjectType<Author>>()
.Resolve(_authors.Take(1).AsExecutable())
.Resolve(_authors.Take(1))
.UseSingleOrDefault())
.BuildRequestExecutorAsync();

Expand Down Expand Up @@ -95,7 +95,7 @@ public async Task ExecuteAsync_Should_Fail_When_SingleOrDefaultMoreThanOne()
.Name("Query")
.Field("executable")
.Type<ObjectType<Author>>()
.Resolve(_authors.AsDbContextExecutable())
.Resolve(_authors)
.UseSingleOrDefault()
.UseProjection()
.UseFiltering()
Expand Down Expand Up @@ -130,7 +130,7 @@ public async Task ExecuteAsync_Should_ReturnNull_When_SingleOrDefaultZero()
.Name("Query")
.Field("executable")
.Type<ObjectType<Author>>()
.Resolve(_authors.Take(0).AsExecutable())
.Resolve(_authors.Take(0))
.UseSingleOrDefault()
.UseProjection()
.UseFiltering()
Expand Down Expand Up @@ -165,7 +165,7 @@ public async Task ExecuteAsync_Should_OnlyOneItem_When_FirstOrDefault()
.Name("Query")
.Field("executable")
.Type<ObjectType<Author>>()
.Resolve(_authors.AsDbContextExecutable())
.Resolve(_authors)
.UseFirstOrDefault())
.BuildRequestExecutorAsync();

Expand Down Expand Up @@ -197,7 +197,7 @@ public async Task ExecuteAsync_Should_ReturnNull_When_FirstOrDefaultZero()
.Name("Query")
.Field("executable")
.Type<ObjectType<ZeroAuthor>>()
.Resolve(_zeroAuthors.Take(0).AsExecutable())
.Resolve(_zeroAuthors.Take(0))
.UseFirstOrDefault()
.UseProjection()
.UseFiltering()
Expand Down
12 changes: 6 additions & 6 deletions src/HotChocolate/Data/test/Data.Tests/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public async Task ExecuteAsync_Should_ReturnAllItems_When_ToListAsync()
x => x
.Name("Query")
.Field("executable")
.Resolve(_authors.AsExecutable())
.Resolve(_authors)
.UseProjection()
.UseFiltering()
.UseSorting())
Expand Down Expand Up @@ -62,7 +62,7 @@ public async Task ExecuteAsync_Should_OnlyOneItem_When_SingleOrDefault()
.Name("Query")
.Field("executable")
.Type<ObjectType<Author>>()
.Resolve(_authors.Take(1).AsExecutable())
.Resolve(_authors.Take(1))
.UseSingleOrDefault()
.UseProjection()
.UseFiltering()
Expand Down Expand Up @@ -97,7 +97,7 @@ public async Task ExecuteAsync_Should_Fail_When_SingleOrDefaultMoreThanOne()
.Name("Query")
.Field("executable")
.Type<ObjectType<Author>>()
.Resolve(_authors.AsExecutable())
.Resolve(_authors)
.UseSingleOrDefault()
.UseProjection()
.UseFiltering()
Expand Down Expand Up @@ -132,7 +132,7 @@ public async Task ExecuteAsync_Should_Fail_When_SingleOrDefaultZero()
.Name("Query")
.Field("executable")
.Type<ObjectType<Author>>()
.Resolve(_authors.Take(0).AsExecutable())
.Resolve(_authors.Take(0))
.UseSingleOrDefault()
.UseProjection()
.UseFiltering()
Expand Down Expand Up @@ -167,7 +167,7 @@ public async Task ExecuteAsync_Should_OnlyOneItem_When_FirstOrDefault()
.Name("Query")
.Field("executable")
.Type<ObjectType<Author>>()
.Resolve(_authors.AsExecutable())
.Resolve(_authors)
.UseFirstOrDefault()
.UseProjection()
.UseFiltering()
Expand Down Expand Up @@ -202,7 +202,7 @@ public async Task ExecuteAsync_Should_OnlyOneItem_When_FirstOrDefaultZero()
.Name("Query")
.Field("executable")
.Type<ObjectType<Author>>()
.Resolve(_authors.Take(0).AsExecutable())
.Resolve(_authors.Take(0))
.UseFirstOrDefault()
.UseProjection()
.UseFiltering()
Expand Down
Loading