Skip to content

Commit bf847d2

Browse files
authored
Fix #6545 sorting variables for expression object sort fields (#9250)
1 parent d333c52 commit bf847d2

3 files changed

Lines changed: 142 additions & 0 deletions

File tree

src/HotChocolate/Data/src/Data/Sorting/Fields/SortField.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,9 @@ protected override void OnCompleteField(
4545
{
4646
RuntimeType = context.TypeInspector.GetType(lambda.ReturnType);
4747
}
48+
else if (base.RuntimeType is { } runtimeType)
49+
{
50+
RuntimeType = context.TypeInspector.GetType(runtimeType);
51+
}
4852
}
4953
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
using HotChocolate.Execution;
2+
using HotChocolate.Types;
3+
using Microsoft.Extensions.DependencyInjection;
4+
5+
namespace HotChocolate.Data.Sorting;
6+
7+
public class Issue6545ReproTests
8+
{
9+
[Fact]
10+
public async Task ExecuteAsync_Should_Sort_When_ExpressionObjectFieldIsPassedAsVariable()
11+
{
12+
Subject[] subjects =
13+
[
14+
new Subject(
15+
"Subject-B",
16+
[
17+
new Address(AddressType.LegalResidential, "Zurich")
18+
]),
19+
new Subject(
20+
"Subject-A",
21+
[
22+
new Address(AddressType.LegalResidential, "Amsterdam")
23+
])
24+
];
25+
26+
var executor = await new ServiceCollection()
27+
.AddGraphQL()
28+
.AddSorting()
29+
.AddQueryType(
30+
descriptor =>
31+
{
32+
descriptor
33+
.Name("Query")
34+
.Field("root")
35+
.Resolve(subjects)
36+
.UseSorting<SubjectSortInputType>();
37+
})
38+
.BuildRequestExecutorAsync(cancellationToken: TestContext.Current.CancellationToken);
39+
40+
var result = await executor.ExecuteAsync(
41+
OperationRequestBuilder.New()
42+
.SetDocument(
43+
"""
44+
query Test($order: [SubjectSortInput!]) {
45+
root(order: $order) {
46+
name
47+
}
48+
}
49+
""")
50+
.SetVariableValues(
51+
"""
52+
{
53+
"order": [
54+
{
55+
"legalResidentialAddress": {
56+
"cityName": "ASC"
57+
}
58+
}
59+
]
60+
}
61+
""")
62+
.Build(),
63+
TestContext.Current.CancellationToken);
64+
65+
result.MatchInlineSnapshot(
66+
"""
67+
{
68+
"data": {
69+
"root": [
70+
{
71+
"name": "Subject-A"
72+
},
73+
{
74+
"name": "Subject-B"
75+
}
76+
]
77+
}
78+
}
79+
""");
80+
}
81+
82+
public class SubjectSortInputType : SortInputType<Subject>
83+
{
84+
protected override void Configure(ISortInputTypeDescriptor<Subject> descriptor)
85+
{
86+
descriptor.BindFieldsImplicitly();
87+
descriptor
88+
.Field(x =>
89+
x.Addresses.FirstOrDefault(address => address.AddressType == AddressType.LegalResidential))
90+
.Name("legalResidentialAddress")
91+
.Type<AddressSortInputType>();
92+
}
93+
}
94+
95+
public class AddressSortInputType : SortInputType<Address>;
96+
97+
public class Subject(string name, IReadOnlyList<Address> addresses)
98+
{
99+
public string Name { get; } = name;
100+
101+
public IReadOnlyList<Address> Addresses { get; } = addresses;
102+
}
103+
104+
public class Address(AddressType addressType, string cityName)
105+
{
106+
public AddressType AddressType { get; } = addressType;
107+
108+
public string CityName { get; } = cityName;
109+
}
110+
111+
public enum AddressType
112+
{
113+
LegalResidential
114+
}
115+
}

src/HotChocolate/Data/test/Data.Sorting.Tests/SortInputTypeTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,29 @@ public void SortInputType_Field_ArrayLengthExpression_Infers_IntRuntimeType()
169169
Assert.Equal(typeof(int), lengthField.RuntimeType?.Source);
170170
}
171171

172+
[Fact]
173+
public void SortInputType_Should_PreserveConfiguredRuntimeType_When_FieldIsNamed()
174+
{
175+
// arrange
176+
var schema = CreateSchema(
177+
s => s.AddType(
178+
new SortInputType<Foo>(
179+
d => d
180+
.Field("custom")
181+
.Type<StringType>()
182+
.Extend()
183+
.OnBeforeCreate(
184+
configuration =>
185+
configuration.Handler = new MatchAnyQueryableFieldHandler()))));
186+
187+
// act
188+
var sortType = Assert.IsType<SortInputType<Foo>>(schema.Types["FooSortInput"]);
189+
190+
// assert
191+
var field = Assert.IsType<SortField>(sortType.Fields["custom"]);
192+
Assert.Equal(typeof(string), field.RuntimeType?.Source);
193+
}
194+
172195
[Fact]
173196
public void SortInputType_Should_ThrowException_WhenNoConventionIsRegistered()
174197
{

0 commit comments

Comments
 (0)