Skip to content
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 @@ -212,6 +212,12 @@ private void CollectTypes(Context context, Selection selection, TypeContainer pa

var assignmentList = assignments.ToImmutable();

// Wee keep EF constructor-injected entities intact by reusing the existing instance.
if (ShouldReuseExistingInstance(context.ParentType))
{
return context.Parent;
}

// Preferred path for mutable types.
var parameterlessConstructor = context.ParentType.GetConstructor(Type.EmptyTypes);
if (parameterlessConstructor is not null)
Expand Down Expand Up @@ -444,6 +450,11 @@ private readonly record struct Context(
}
}

private static bool ShouldReuseExistingInstance(Type type)
=> type.GetConstructor(Type.EmptyTypes) is not null
&& type.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic)
.Any(t => t.GetParameters().Length > 0);

private static bool IsMarkedAsExplicitlyNonNullable(ParameterInfo parameter)
=> new NullabilityInfoContext().Create(parameter).WriteState is NullabilityState.NotNull;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using HotChocolate.Execution;
using HotChocolate.Execution.Processing;
using HotChocolate.Types;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -534,12 +535,90 @@ static Dictionary<string, int> ReadCounts(JsonElement value)
}
}

[Fact]
public async Task AsSelector_Should_Preserve_Entity_Constructor_DbContext_Injection()
{
var databaseName = $"db-{Guid.NewGuid():N}";

await using (var seedContext = new ConstructorInjectionDbContext(
new DbContextOptionsBuilder<ConstructorInjectionDbContext>()
.UseInMemoryDatabase(databaseName)
.Options))
{
await seedContext.Database.EnsureCreatedAsync();

var blog1 = new ConstructorInjectionBlog { Name = "Blog1" };
var blog2 = new ConstructorInjectionBlog { Name = "Blog2" };

await seedContext.Blogs.AddRangeAsync(blog1, blog2);
await seedContext.SaveChangesAsync();

await seedContext.Posts.AddRangeAsync(
new ConstructorInjectionPost { BlogId = blog1.Id },
new ConstructorInjectionPost { BlogId = blog1.Id },
new ConstructorInjectionPost { BlogId = blog2.Id });
await seedContext.SaveChangesAsync();
}

var executor = await new ServiceCollection()
.AddDbContext<ConstructorInjectionDbContext>(
b => b.UseInMemoryDatabase(databaseName))
.AddGraphQL()
.AddProjections()
.AddQueryType<ConstructorInjectionQuery>()
.BuildRequestExecutorAsync();

var result = await executor.ExecuteAsync(
"""
{
blogsAsSelector {
name
postCount
}
blogsNoProjection {
name
postCount
}
}
""");

var operationResult = result.ExpectOperationResult();
Assert.True(operationResult.Errors is null || operationResult.Errors.Count == 0);
Assert.True(operationResult.Data.HasValue);

using var document = JsonDocument.Parse(result.ToJson());
var data = document.RootElement.GetProperty("data");
var projectedCounts = ReadCounts(data.GetProperty("blogsAsSelector"));
var unprojectedCounts = ReadCounts(data.GetProperty("blogsNoProjection"));

Assert.Equal(unprojectedCounts, projectedCounts);
Assert.Equal(3, projectedCounts["Blog1"]);
Assert.Equal(3, projectedCounts["Blog2"]);

static Dictionary<string, int> ReadCounts(JsonElement value)
{
var result = new Dictionary<string, int>();

foreach (var item in value.EnumerateArray())
{
result.Add(item.GetProperty("name").GetString()!, item.GetProperty("postCount").GetInt32());
}

return result;
}
}

public class ConstructorInjectionQuery
{
[UseProjection]
public IQueryable<ConstructorInjectionBlog> GetBlogs(ConstructorInjectionDbContext context)
=> context.Blogs;

public IQueryable<ConstructorInjectionBlog> GetBlogsAsSelector(
ConstructorInjectionDbContext context,
ISelection selection)
=> context.Blogs.Select(selection.AsSelector<ConstructorInjectionBlog>());

public IQueryable<ConstructorInjectionBlog> GetBlogsNoProjection(
ConstructorInjectionDbContext context)
=> context.Blogs;
Expand Down