Skip to content

Commit

Permalink
Apply limit to IQueryable in FirstOrDefault/SingleOrDefaultMiddleware
Browse files Browse the repository at this point in the history
  • Loading branch information
glen-84 committed Jan 17, 2024
1 parent ea4430b commit 5626eda
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ public async Task InvokeAsync(IMiddlewareContext context)
{
case IAsyncEnumerable<T> ae:
{
// Apply limit.
if (ae is IQueryable<T> q)

Check warning on line 33 in src/HotChocolate/Data/src/Data/Projections/FirstOrDefaultMiddleware~1.cs

View check run for this annotation

Codecov / codecov/patch

src/HotChocolate/Data/src/Data/Projections/FirstOrDefaultMiddleware~1.cs#L32-L33

Added lines #L32 - L33 were not covered by tests
{
q = q.Take(1);

ae = (IAsyncEnumerable<T>)q;
}

Check warning on line 38 in src/HotChocolate/Data/src/Data/Projections/FirstOrDefaultMiddleware~1.cs

View check run for this annotation

Codecov / codecov/patch

src/HotChocolate/Data/src/Data/Projections/FirstOrDefaultMiddleware~1.cs#L35-L38

Added lines #L35 - L38 were not covered by tests

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

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

Check warning on line 31 in src/HotChocolate/Data/src/Data/Projections/SingleOrDefaultMiddleware~1.cs

View check run for this annotation

Codecov / codecov/patch

src/HotChocolate/Data/src/Data/Projections/SingleOrDefaultMiddleware~1.cs#L30-L31

Added lines #L30 - L31 were not covered by tests
{
q = q.Take(2);

ae = (IAsyncEnumerable<T>)q;
}

Check warning on line 36 in src/HotChocolate/Data/src/Data/Projections/SingleOrDefaultMiddleware~1.cs

View check run for this annotation

Codecov / codecov/patch

src/HotChocolate/Data/src/Data/Projections/SingleOrDefaultMiddleware~1.cs#L33-L36

Added lines #L33 - L36 were not covered by tests

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public async Task ExecuteAsync_Should_ReturnAllItems_When_ToListAsync()
x => x
.Name("Query")
.Field("executable")
.Resolve(_authors.AsExecutable()))
.Resolve(_authors))
.BuildRequestExecutorAsync();

// act
Expand Down Expand Up @@ -65,7 +65,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 @@ -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_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 @@ -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())
.BuildRequestExecutorAsync();

Expand Down Expand Up @@ -199,7 +199,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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
"errors": [
{
"message": "Sequence contains more than one element.",
"locations": [
{
"line": 3,
"column": 21
}
],
"path": [
"executable"
],
"extensions": {
"code": "HC0022"
}
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 @@ -31,7 +31,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 @@ -65,7 +65,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 @@ -100,7 +100,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 @@ -135,7 +135,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 @@ -170,7 +170,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 @@ -205,7 +205,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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
"errors": [
{
"message": "Sequence contains more than one element.",
"locations": [
{
"line": 2,
"column": 5
}
],
"path": [
"executable"
],
"extensions": {
"code": "HC0022"
}
Expand Down

0 comments on commit 5626eda

Please sign in to comment.