Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -163,7 +163,7 @@ private void CollectTypes(Context context, ISelection selection, TypeContainer p
return BuildSelectionSetExpression(context, parent.Nodes[0]);
}

private static MemberInitExpression? BuildSelectionSetExpression(
private static Expression? BuildSelectionSetExpression(
Context context,
TypeNode parent)
{
Expand All @@ -183,9 +183,63 @@ private void CollectTypes(Context context, ISelection selection, TypeContainer p
return null;
}

return Expression.MemberInit(
Expression.New(context.ParentType),
assignments.ToImmutable());
var assignmentList = assignments.ToImmutable();

// Quick path: parameterless constructor + MemberInit
var parameterlessCtor = context.ParentType.GetConstructor(Type.EmptyTypes);
if (parameterlessCtor != null)
{
var allWritable = assignmentList.All(a =>
a.Member is PropertyInfo { CanWrite: true, SetMethod.IsPublic: true });

if (allWritable)
{
return Expression.MemberInit(
Expression.New(parameterlessCtor),
assignmentList);
}
}

// Fallback: Use the best matching constructor.
// Argument names must match parameter names (case-insensitive),
// and argument types must be assignable to the parameter types.
ConstructorInfo? bestMatchingCtor = null;
ParameterInfo[]? ctorParams = null;
foreach (var ctor in context.ParentType.GetConstructors())
{
var parameters = ctor.GetParameters();
if (parameters.Length == assignmentList.Length)
{
var argumentsMatchParameters = parameters.All(p =>
assignmentList.Any(a =>
string.Equals(a.Member.Name, p.Name, StringComparison.OrdinalIgnoreCase)
&& a.Expression.Type.IsAssignableTo(p.ParameterType)));

if (argumentsMatchParameters)
{
bestMatchingCtor = ctor;
ctorParams = parameters;
break;
}
}
}

if (bestMatchingCtor != null && ctorParams != null)
{
// args and params might not match order -> align them
var args = ctorParams.Select(p =>
{
var member = assignmentList.First(a =>
string.Equals(a.Member.Name, p.Name, StringComparison.OrdinalIgnoreCase));

return Expression.Convert(member.Expression, p.ParameterType);
}).ToArray();

return Expression.New(bestMatchingCtor, args);
}

throw new InvalidOperationException(
$"No writable properties or suitable constructor found for type '{context.ParentType.Name}'.");
}

private void CollectSelection(
Expand Down
51 changes: 51 additions & 0 deletions src/HotChocolate/Data/test/Data.Tests/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using HotChocolate.Data.Sorting;
using HotChocolate.Execution;
using HotChocolate.Types;
using HotChocolate.Types.Pagination;
using Microsoft.Extensions.DependencyInjection;

namespace HotChocolate.Data;
Expand Down Expand Up @@ -960,6 +961,37 @@ public async Task AsSortDefinition_Descending_QueryContext_2()
result.MatchSnapshot();
}

[Fact]
public async Task UsingQueryContext_ShouldNotBreak_Pagination_ForRecordReturnType()
{
// arrange
var executor = await new ServiceCollection()
.AddGraphQL()
.AddFiltering()
.AddSorting()
.AddProjections()
.AddQueryType<RecordQuery>()
.BuildRequestExecutorAsync();

// act
var result = await executor.ExecuteAsync(
"""
query f {
users {
edges {
node {
firstName
id
}
}
}
}
""");

// assert
result.MatchSnapshot();
}

[QueryType]
public static class StaticQuery
{
Expand Down Expand Up @@ -1214,4 +1246,23 @@ public IQueryable<Author> GetAuthorsData2(QueryContext<Author> context)
}.AsQueryable()
.With(context, t => t with { Operations = t.Operations.Add(SortBy<Author>.Ascending(t => t.Id)) });
}

#pragma warning disable RCS1102
public class RecordQuery
#pragma warning restore RCS1102
{
[UsePaging]
[UseFiltering]
public Connection<User> GetUsers(
QueryContext<User> query
)
{
return Connection.Empty<User>();
}

public record User(
string Id,
string FirstName
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"data": {
"users": {
"edges": []
}
}
}