Skip to content

Commit 77903fa

Browse files
committed
Fixes the sort order in GraphQL queries. (#17476)
1 parent 5e4432b commit 77903fa

File tree

3 files changed

+82
-1
lines changed

3 files changed

+82
-1
lines changed

src/OrchardCore/OrchardCore.ContentManagement.GraphQL/Queries/ContentItemsFieldType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ private static IQuery<ContentItem, ContentItemIndex> OrderBy(IQuery<ContentItem,
362362

363363
foreach (var property in orderByArguments)
364364
{
365-
var direction = (OrderByDirection)property.Value.Value<int>();
365+
var direction = property.Value.GetEnumValue<OrderByDirection>();
366366

367367
Expression<Func<ContentItemIndex, object>> selector = null;
368368

test/OrchardCore.Tests/Apis/GraphQL/ContentManagement/DynamicContentTypeQueryTests.cs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,83 @@ public async Task ShouldQueryMultipleContentFields()
109109

110110
Assert.Equal(context.Product2ContentItemId, result["data"]["product"].AsArray().First()["contentItemId"].ToString());
111111
}
112+
113+
[Fact]
114+
public async Task ShouldOrderByCreatedUtc()
115+
{
116+
using var context = new DynamicContentTypeContext();
117+
await context.InitializeAsync();
118+
119+
var resultAsc = await context
120+
.GraphQLClient
121+
.Content
122+
.Query(@"product(orderBy: {createdUtc: ASC}) {
123+
contentItemId
124+
displayText
125+
createdUtc
126+
}");
127+
128+
var resultDesc = await context
129+
.GraphQLClient
130+
.Content
131+
.Query(@"product(orderBy: {createdUtc: DESC}) {
132+
contentItemId
133+
displayText
134+
createdUtc
135+
}");
136+
137+
Assert.Equal(new DateTime(2024, 04, 07, 0, 0, 0, DateTimeKind.Utc), resultAsc["data"]["product"].AsArray().First()["createdUtc"].GetValue<DateTime>());
138+
Assert.Equal(new DateTime(2024, 05, 18, 0, 0, 0, DateTimeKind.Utc), resultDesc["data"]["product"].AsArray().First()["createdUtc"].GetValue<DateTime>());
139+
}
140+
141+
[Fact]
142+
public async Task ShouldQuerySimilarNamedContentFields()
143+
{
144+
using var context = new DynamicContentTypeContext();
145+
await context.InitializeAsync();
146+
147+
var numberResult = await context
148+
.GraphQLClient
149+
.Content
150+
.Query(@"numberType(where: {value: 123}) {
151+
contentItemId
152+
displayText
153+
value
154+
}");
155+
156+
var stringResult = await context
157+
.GraphQLClient
158+
.Content
159+
.Query(@"stringType(where: {value: ""Text123""}) {
160+
contentItemId
161+
displayText
162+
value
163+
}");
164+
165+
Assert.Single(numberResult["data"]["numberType"].AsArray());
166+
Assert.Equal("123", numberResult["data"]["numberType"].AsArray().First()["value"].ToString());
167+
168+
Assert.Single(stringResult["data"]["stringType"].AsArray());
169+
Assert.Equal("Text123", stringResult["data"]["stringType"].AsArray().First()["value"].ToString());
170+
}
171+
172+
[Fact]
173+
public async Task ShouldDistinquishMultipleNamedContentFields()
174+
{
175+
using var context = new DynamicContentTypeContext();
176+
await context.InitializeAsync();
177+
178+
// Make sure values of field2 are not included by querying field1
179+
var result = await context
180+
.GraphQLClient
181+
.Content
182+
.Query(@"twoNumbersType(where: {field1: 200}) {
183+
contentItemId
184+
displayText
185+
field1
186+
field2
187+
}");
188+
189+
Assert.Empty(result["data"]["twoNumbersType"].AsArray());
190+
}
112191
}

test/OrchardCore.Tests/Apis/GraphQL/ContentManagement/Recipes/DynamicContentTypeQueryTest.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@
150150
"DisplayText": "Product 1",
151151
"Latest": true,
152152
"Published": true,
153+
"CreatedUtc": "2024-04-07T00:00:00Z",
153154
"PricePart": {
154155
"Amount": {
155156
"Value": 10
@@ -175,6 +176,7 @@
175176
"DisplayText": "Product 2",
176177
"Latest": true,
177178
"Published": true,
179+
"CreatedUtc": "2024-05-18T00:00:00Z",
178180
"PricePart": {
179181
"Amount": {
180182
"Value": 22

0 commit comments

Comments
 (0)