Skip to content

Commit bc659e4

Browse files
authored
Add tests for sorting over proj queryables with upstream OrderBy (#9216)
1 parent 21fd838 commit bc659e4

1 file changed

Lines changed: 70 additions & 0 deletions

File tree

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

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,58 @@ await Snapshot
8888
.MatchAsync(TestContext.Current.CancellationToken);
8989
}
9090

91+
[Fact]
92+
public async Task Sorting_Should_Apply_OrderBy_When_Query_Is_Projected()
93+
{
94+
// arrange
95+
var executor = await new ServiceCollection()
96+
.AddGraphQL()
97+
.AddQueryType<Query>()
98+
.AddSorting()
99+
.ModifyRequestOptions(o => o.IncludeExceptionDetails = true)
100+
.BuildRequestExecutorAsync(cancellationToken: TestContext.Current.CancellationToken);
101+
102+
// act
103+
var result = await executor.ExecuteAsync(
104+
OperationRequestBuilder
105+
.New()
106+
.SetDocument(
107+
"""
108+
{
109+
projectedWithExistingOrder(order: { someProperty: DESC }) {
110+
someProperty
111+
}
112+
}
113+
""")
114+
.Build(),
115+
TestContext.Current.CancellationToken);
116+
117+
// assert
118+
result.MatchInlineSnapshot(
119+
"""
120+
{
121+
"data": {
122+
"projectedWithExistingOrder": [
123+
{
124+
"someProperty": "b"
125+
},
126+
{
127+
"someProperty": "a"
128+
}
129+
]
130+
}
131+
}
132+
""");
133+
}
134+
91135
public class Query
92136
{
137+
private static readonly Source[] s_source =
138+
[
139+
new() { SomeProperty = "a", CreateDate = new DateTime(2020, 1, 1) },
140+
new() { SomeProperty = "b", CreateDate = new DateTime(2021, 1, 1) }
141+
];
142+
93143
[UseSorting]
94144
public IEnumerable<Foo> ShouldWork(IResolverContext context)
95145
{
@@ -109,6 +159,15 @@ public IEnumerable<Foo> MissingMiddleware(IResolverContext context)
109159
{
110160
return s_fooEntities.Sort(context);
111161
}
162+
163+
[UseSorting]
164+
public IQueryable<Projection> ProjectedWithExistingOrder()
165+
{
166+
return s_source
167+
.AsQueryable()
168+
.OrderBy(x => x.CreateDate)
169+
.Select(x => new Projection { SomeProperty = x.SomeProperty });
170+
}
112171
}
113172

114173
public class Foo
@@ -124,6 +183,17 @@ public class Foo
124183
public string? NotSettable { get; }
125184
}
126185

186+
public class Source
187+
{
188+
public string SomeProperty { get; set; } = default!;
189+
public DateTime CreateDate { get; set; }
190+
}
191+
192+
public class Projection
193+
{
194+
public string SomeProperty { get; set; } = default!;
195+
}
196+
127197
public class AddTypeMismatchMiddlewareAttribute : ObjectFieldDescriptorAttribute
128198
{
129199
protected override void OnConfigure(

0 commit comments

Comments
 (0)