-
-
Notifications
You must be signed in to change notification settings - Fork 756
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added more include overloads. (#8002)
- Loading branch information
1 parent
77b8d12
commit 6878fe9
Showing
14 changed files
with
420 additions
and
28 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
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
1 change: 1 addition & 0 deletions
1
...te/Core/src/Execution.Projections/Extensions/HotChocolateExecutionDataLoaderExtensions.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
144 changes: 144 additions & 0 deletions
144
src/HotChocolate/Data/test/Data.PostgreSQL.Tests/DataLoaderTests.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,144 @@ | ||
using GreenDonut; | ||
using GreenDonut.Data; | ||
using HotChocolate.Data.Data; | ||
using HotChocolate.Data.Migrations; | ||
using HotChocolate.Data.Models; | ||
using HotChocolate.Data.Services; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Squadron; | ||
|
||
namespace HotChocolate.Data; | ||
|
||
[Collection(PostgresCacheCollectionFixture.DefinitionName)] | ||
public class DataLoaderTests(PostgreSqlResource resource) | ||
{ | ||
[Fact] | ||
public async Task Include_On_List_Results() | ||
{ | ||
// arrange | ||
using var interceptor = new TestQueryInterceptor(); | ||
using var cts = new CancellationTokenSource(2000); | ||
await using var services = CreateServer(); | ||
await using var scope = services.CreateAsyncScope(); | ||
await using var context = scope.ServiceProvider.GetRequiredService<CatalogContext>(); | ||
var seeder = scope.ServiceProvider.GetRequiredService<IDbSeeder<CatalogContext>>(); | ||
await context.Database.EnsureCreatedAsync(cts.Token); | ||
await seeder.SeedAsync(context); | ||
|
||
var productByBrand = scope.ServiceProvider.GetRequiredService<IProductListByBrandDataLoader>(); | ||
|
||
// act | ||
var products = await productByBrand | ||
.Select(t => new Product { BrandId = t.BrandId, Name = t.Name }) | ||
.Include(t => t.Price) | ||
.LoadRequiredAsync(1, cts.Token); | ||
|
||
// assert | ||
Assert.Equal(10, products.Count); | ||
interceptor.MatchSnapshot(); | ||
} | ||
|
||
[Fact] | ||
public async Task Include_On_Array_Results() | ||
{ | ||
// arrange | ||
using var interceptor = new TestQueryInterceptor(); | ||
using var cts = new CancellationTokenSource(2000); | ||
await using var services = CreateServer(); | ||
await using var scope = services.CreateAsyncScope(); | ||
await using var context = scope.ServiceProvider.GetRequiredService<CatalogContext>(); | ||
var seeder = scope.ServiceProvider.GetRequiredService<IDbSeeder<CatalogContext>>(); | ||
await context.Database.EnsureCreatedAsync(cts.Token); | ||
await seeder.SeedAsync(context); | ||
|
||
var productByBrand = scope.ServiceProvider.GetRequiredService<IProductArrayByBrandDataLoader>(); | ||
|
||
// act | ||
var products = await productByBrand | ||
.Select(t => new Product { BrandId = t.BrandId, Name = t.Name }) | ||
.Include(t => t.Price) | ||
.LoadRequiredAsync(1, cts.Token); | ||
|
||
// assert | ||
Assert.Equal(10, products.Length); | ||
interceptor.MatchSnapshot(); | ||
} | ||
|
||
[Fact] | ||
public async Task Include_On_Page_Results() | ||
{ | ||
// arrange | ||
using var interceptor = new TestQueryInterceptor(); | ||
using var cts = new CancellationTokenSource(2000); | ||
await using var services = CreateServer(); | ||
await using var scope = services.CreateAsyncScope(); | ||
await using var context = scope.ServiceProvider.GetRequiredService<CatalogContext>(); | ||
var seeder = scope.ServiceProvider.GetRequiredService<IDbSeeder<CatalogContext>>(); | ||
await context.Database.EnsureCreatedAsync(cts.Token); | ||
await seeder.SeedAsync(context); | ||
|
||
var productByBrand = scope.ServiceProvider.GetRequiredService<IProductsByBrandDataLoader>(); | ||
|
||
// act | ||
var products = await productByBrand | ||
.With(new PagingArguments { First = 5 }) | ||
.Select(t => new Product { BrandId = t.BrandId, Name = t.Name }) | ||
.Include(t => t.Price) | ||
.LoadRequiredAsync(1, cts.Token); | ||
|
||
// assert | ||
Assert.Equal(5, products.Items.Length); | ||
interceptor.MatchSnapshot(); | ||
} | ||
|
||
private ServiceProvider CreateServer() | ||
{ | ||
var db = "db_" + Guid.NewGuid().ToString("N"); | ||
var connectionString = resource.GetConnectionString(db); | ||
|
||
var services = new ServiceCollection(); | ||
|
||
services | ||
.AddLogging() | ||
.AddDbContext<CatalogContext>(c => c.UseNpgsql(connectionString)); | ||
|
||
services | ||
.AddSingleton<BrandService>() | ||
.AddSingleton<ProductService>(); | ||
|
||
services | ||
.AddGraphQLServer() | ||
.AddCustomTypes() | ||
.AddGlobalObjectIdentification() | ||
.AddPagingArguments() | ||
.AddFiltering() | ||
.AddSorting() | ||
.ModifyRequestOptions(o => o.IncludeExceptionDetails = true); | ||
|
||
services.AddSingleton<IDbSeeder<CatalogContext>, CatalogContextSeed>(); | ||
|
||
return services.BuildServiceProvider(); | ||
} | ||
} | ||
|
||
file static class Extensions | ||
{ | ||
public static void MatchSnapshot( | ||
this TestQueryInterceptor queryInterceptor) | ||
{ | ||
#if NET9_0_OR_GREATER | ||
var snapshot = Snapshot.Create(); | ||
#else | ||
var snapshot = Snapshot.Create(postFix: "_net_8_0"); | ||
#endif | ||
|
||
for (var i = 0; i < queryInterceptor.Queries.Count; i++) | ||
{ | ||
var sql = queryInterceptor.Queries[i]; | ||
snapshot.Add(sql, $"Query {i + 1}", MarkdownLanguages.Sql); | ||
} | ||
|
||
snapshot.MatchMarkdown(); | ||
} | ||
} |
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
Oops, something went wrong.