-
-
Notifications
You must be signed in to change notification settings - Fork 749
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added refinements to the ef core helpers.
- Loading branch information
1 parent
3070cde
commit d6da908
Showing
15 changed files
with
273 additions
and
35 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
using System; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
|
3 changes: 0 additions & 3 deletions
3
src/HotChocolate/Data/src/EntityFramework/DbContextParameterExpressionBuilder.cs
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
21 changes: 7 additions & 14 deletions
21
src/HotChocolate/Data/src/EntityFramework/EntityFrameworkExecutable.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,20 @@ | ||
using System.Collections; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace HotChocolate.Data; | ||
|
||
public class EntityFrameworkExecutable<T> : QueryableExecutable<T> | ||
public class EntityFrameworkExecutable<T>(IQueryable<T> queryable) : QueryableExecutable<T>(queryable) | ||
{ | ||
public EntityFrameworkExecutable(IQueryable<T> queryable) : base(queryable) | ||
{ | ||
} | ||
|
||
public override async ValueTask<IList> ToListAsync(CancellationToken cancellationToken) => | ||
await Source.ToListAsync(cancellationToken).ConfigureAwait(false); | ||
public override async ValueTask<IList> ToListAsync(CancellationToken cancellationToken) | ||
=> await Source.ToListAsync(cancellationToken).ConfigureAwait(false); | ||
|
||
public override async ValueTask<object?> FirstOrDefaultAsync( | ||
CancellationToken cancellationToken) => | ||
await Source.FirstOrDefaultAsync(cancellationToken).ConfigureAwait(false); | ||
CancellationToken cancellationToken) | ||
=> await Source.FirstOrDefaultAsync(cancellationToken).ConfigureAwait(false); | ||
|
||
public override async ValueTask<object?> SingleOrDefaultAsync( | ||
CancellationToken cancellationToken) => | ||
await Source.SingleOrDefaultAsync(cancellationToken).ConfigureAwait(false); | ||
CancellationToken cancellationToken) | ||
=> await Source.SingleOrDefaultAsync(cancellationToken).ConfigureAwait(false); | ||
|
||
public override string Print() => Source.ToQueryString(); | ||
} |
1 change: 0 additions & 1 deletion
1
src/HotChocolate/Data/src/EntityFramework/Extensions/EntityFrameworkEnumerableExtensions.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
using System.Linq; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace HotChocolate.Data; | ||
|
2 changes: 0 additions & 2 deletions
2
...ate/Data/src/EntityFramework/Extensions/EntityFrameworkObjectFieldDescriptorExtensions.cs
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,4 +33,4 @@ public static TDbContext DbContext<TDbContext>(this IResolverContext context) | |
|
||
return casted; | ||
} | ||
} | ||
} |
1 change: 0 additions & 1 deletion
1
src/HotChocolate/Data/src/EntityFramework/Extensions/ExecutableMiddleware.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
using System.Threading.Tasks; | ||
using HotChocolate.Resolvers; | ||
|
||
namespace HotChocolate.Types; | ||
|
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
2 changes: 0 additions & 2 deletions
2
src/HotChocolate/Data/src/EntityFramework/InferDbContextParameterExpressionBuilder.cs
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
3 changes: 0 additions & 3 deletions
3
src/HotChocolate/Data/src/EntityFramework/ToListMiddleware.cs
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
1 change: 0 additions & 1 deletion
1
src/HotChocolate/Data/src/EntityFramework/UseDbContextAttribute.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
using System; | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
using HotChocolate.Types; | ||
|
131 changes: 131 additions & 0 deletions
131
src/HotChocolate/Data/test/Data.EntityFramework.Tests/PagingHelperIntegrationTests.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
using HotChocolate.Data.TestContext; | ||
using CookieCrumble; | ||
using HotChocolate.Execution; | ||
using HotChocolate.Resolvers; | ||
using HotChocolate.Types; | ||
using HotChocolate.Types.Pagination; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Squadron; | ||
|
||
namespace HotChocolate.Data; | ||
|
||
public class IntegrationPagingHelperTests(PostgreSqlResource resource) : IClassFixture<PostgreSqlResource> | ||
{ | ||
public PostgreSqlResource Resource { get; } = resource; | ||
|
||
private string CreateConnectionString() | ||
=> Resource.GetConnectionString($"db_{Guid.NewGuid():N}"); | ||
|
||
[Fact] | ||
public async Task GetDefaultPage() | ||
{ | ||
// Arrange | ||
var connectionString = CreateConnectionString(); | ||
await SeedAsync(connectionString); | ||
|
||
// Act | ||
var result = await new ServiceCollection() | ||
.AddScoped(_ => new CatalogContext(connectionString)) | ||
.AddGraphQL() | ||
.AddQueryType<Query>() | ||
.AddPagingArguments() | ||
.ExecuteRequestAsync( | ||
""" | ||
{ | ||
brands { | ||
nodes { | ||
id | ||
name | ||
} | ||
pageInfo { | ||
hasNextPage | ||
hasPreviousPage | ||
startCursor | ||
endCursor | ||
} | ||
} | ||
} | ||
"""); | ||
|
||
// Assert | ||
result.MatchMarkdownSnapshot(); | ||
} | ||
|
||
[Fact] | ||
public async Task GetSecondPage_With_2_Items() | ||
{ | ||
// Arrange | ||
var connectionString = CreateConnectionString(); | ||
await SeedAsync(connectionString); | ||
|
||
// Act | ||
var result = await new ServiceCollection() | ||
.AddScoped(_ => new CatalogContext(connectionString)) | ||
.AddGraphQL() | ||
.AddQueryType<Query>() | ||
.AddPagingArguments() | ||
.ExecuteRequestAsync( | ||
""" | ||
{ | ||
brands(first: 2, after: "QnJhbmQxNzoxOA==") { | ||
nodes { | ||
id | ||
name | ||
} | ||
pageInfo { | ||
hasNextPage | ||
hasPreviousPage | ||
startCursor | ||
endCursor | ||
} | ||
} | ||
} | ||
"""); | ||
|
||
// Assert | ||
result.MatchMarkdownSnapshot(); | ||
} | ||
|
||
private static async Task SeedAsync(string connectionString) | ||
{ | ||
await using var context = new CatalogContext(connectionString); | ||
await context.Database.EnsureCreatedAsync(); | ||
|
||
var type = new ProductType { Name = "T-Shirt", }; | ||
context.ProductTypes.Add(type); | ||
|
||
for (var i = 0; i < 100; i++) | ||
{ | ||
var brand = new Brand { Name = "Brand" + i, }; | ||
context.Brands.Add(brand); | ||
|
||
for (var j = 0; j < 100; j++) | ||
{ | ||
var product = new Product | ||
{ | ||
Name = $"Product {i}-{j}", | ||
Type = type, | ||
Brand = brand, | ||
}; | ||
context.Products.Add(product); | ||
} | ||
} | ||
|
||
await context.SaveChangesAsync(); | ||
} | ||
|
||
public class Query | ||
{ | ||
[UsePaging] | ||
public async Task<Connection<Brand>> GetBrandsAsync( | ||
CatalogContext context, | ||
PagingArguments arguments, | ||
CancellationToken ct) | ||
=> await context.Brands | ||
.OrderBy(t => t.Name) | ||
.ThenBy(t => t.Id) | ||
.ToPageAsync(arguments, cancellationToken: ct) | ||
.ToConnectionAsync(); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
...ityFramework.Tests/__snapshots__/IntegrationPagingHelperTests.GetDefaultPage.md
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# GetDefaultPage | ||
|
||
```json | ||
{ | ||
"data": { | ||
"brands": { | ||
"nodes": [ | ||
{ | ||
"id": 1, | ||
"name": "Brand0" | ||
}, | ||
{ | ||
"id": 2, | ||
"name": "Brand1" | ||
}, | ||
{ | ||
"id": 11, | ||
"name": "Brand10" | ||
}, | ||
{ | ||
"id": 12, | ||
"name": "Brand11" | ||
}, | ||
{ | ||
"id": 13, | ||
"name": "Brand12" | ||
}, | ||
{ | ||
"id": 14, | ||
"name": "Brand13" | ||
}, | ||
{ | ||
"id": 15, | ||
"name": "Brand14" | ||
}, | ||
{ | ||
"id": 16, | ||
"name": "Brand15" | ||
}, | ||
{ | ||
"id": 17, | ||
"name": "Brand16" | ||
}, | ||
{ | ||
"id": 18, | ||
"name": "Brand17" | ||
} | ||
], | ||
"pageInfo": { | ||
"hasNextPage": true, | ||
"hasPreviousPage": false, | ||
"startCursor": "QnJhbmQwOjE=", | ||
"endCursor": "QnJhbmQxNzoxOA==" | ||
} | ||
} | ||
} | ||
} | ||
``` |
26 changes: 26 additions & 0 deletions
26
....Tests/__snapshots__/IntegrationPagingHelperTests.GetSecondPage_With_2_Items.md
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# GetSecondPage_With_2_Items | ||
|
||
```json | ||
{ | ||
"data": { | ||
"brands": { | ||
"nodes": [ | ||
{ | ||
"id": 19, | ||
"name": "Brand18" | ||
}, | ||
{ | ||
"id": 20, | ||
"name": "Brand19" | ||
} | ||
], | ||
"pageInfo": { | ||
"hasNextPage": true, | ||
"hasPreviousPage": false, | ||
"startCursor": "QnJhbmQxODoxOQ==", | ||
"endCursor": "QnJhbmQxOToyMA==" | ||
} | ||
} | ||
} | ||
} | ||
``` |