Skip to content

Commit e6a2dec

Browse files
committed
add orderby callback
1 parent f686c16 commit e6a2dec

11 files changed

+53
-27
lines changed

src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<Project>
33
<PropertyGroup>
44
<NoWarn>CS1591;NU5104;CS1573;CS9107</NoWarn>
5-
<Version>27.2.2</Version>
5+
<Version>28.0.0-beta.1</Version>
66
<LangVersion>preview</LangVersion>
77
<AssemblyVersion>1.0.0</AssemblyVersion>
88
<PackageTags>EntityFrameworkCore, EntityFramework, GraphQL</PackageTags>

src/GraphQL.EntityFramework/GraphApi/EfGraphQLService_Queryable.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ public FieldBuilder<object, TReturn> AddQueryField<TReturn>(
88
string name,
99
Func<ResolveEfFieldContext<TDbContext, object>, IQueryable<TReturn>>? resolve = null,
1010
Type? graphType = null,
11-
bool omitQueryArguments = false)
11+
bool omitQueryArguments = false,
12+
Func<ResolveEfFieldContext<TDbContext, object>, IQueryable<TReturn>, IOrderedQueryable<TReturn>>? orderBy = null)
1213
where TReturn : class
1314
{
14-
var field = BuildQueryField(graphType, name, resolve, omitQueryArguments);
15+
var field = BuildQueryField(graphType, name, resolve, omitQueryArguments, orderBy);
1516
graph.AddField(field);
1617
return new FieldBuilderEx<object, TReturn>(field);
1718
}
@@ -21,10 +22,11 @@ public FieldBuilder<TSource, TReturn> AddQueryField<TSource, TReturn>(
2122
string name,
2223
Func<ResolveEfFieldContext<TDbContext, TSource>, IQueryable<TReturn>>? resolve = null,
2324
Type? itemGraphType = null,
24-
bool omitQueryArguments = false)
25+
bool omitQueryArguments = false,
26+
Func<ResolveEfFieldContext<TDbContext, TSource>, IQueryable<TReturn>, IOrderedQueryable<TReturn>>? orderBy = null)
2527
where TReturn : class
2628
{
27-
var field = BuildQueryField(itemGraphType, name, resolve, omitQueryArguments);
29+
var field = BuildQueryField(itemGraphType, name, resolve, omitQueryArguments, orderBy);
2830
graph.AddField(field);
2931
return new FieldBuilderEx<TSource, TReturn>(field);
3032
}
@@ -33,7 +35,8 @@ FieldType BuildQueryField<TSource, TReturn>(
3335
Type? itemGraphType,
3436
string name,
3537
Func<ResolveEfFieldContext<TDbContext, TSource>, IQueryable<TReturn>>? resolve,
36-
bool omitQueryArguments)
38+
bool omitQueryArguments,
39+
Func<ResolveEfFieldContext<TDbContext, TSource>, IQueryable<TReturn>, IOrderedQueryable<TReturn>>? orderBy)
3740
where TReturn : class
3841
{
3942
Guard.AgainstWhiteSpace(nameof(name), name);
@@ -65,6 +68,11 @@ FieldType BuildQueryField<TSource, TReturn>(
6568
query = query.ApplyGraphQlArguments(context, names, true, omitQueryArguments);
6669
}
6770

71+
if(orderBy is not null)
72+
{
73+
query = orderBy(fieldContext, query);
74+
}
75+
6876
QueryLogger.Write(query);
6977

7078
List<TReturn> list;

src/GraphQL.EntityFramework/GraphApi/EfInterfaceGraphType.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ public ConnectionBuilder<TSource> AddQueryConnectionField<TReturn>(
3838
public FieldBuilder<object, TReturn> AddQueryField<TReturn>(
3939
string name,
4040
Type? graphType = null,
41-
bool omitQueryArguments = false)
41+
bool omitQueryArguments = false,
42+
Func<ResolveEfFieldContext<TDbContext, object>, IQueryable<TReturn>, IOrderedQueryable<TReturn>>? orderBy = null)
4243
where TReturn : class =>
43-
GraphQlService.AddQueryField<TReturn>(this, name, null, graphType, omitQueryArguments);
44+
GraphQlService.AddQueryField(this, name, null, graphType, omitQueryArguments, orderBy);
4445

4546
public TDbContext ResolveDbContext(IResolveFieldContext<TSource> context) =>
4647
GraphQlService.ResolveDbContext(context);

src/GraphQL.EntityFramework/GraphApi/EfObjectGraphType.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,10 @@ public FieldBuilder<TSource, TReturn> AddQueryField<TReturn>(
5454
string name,
5555
Func<ResolveEfFieldContext<TDbContext, TSource>, IQueryable<TReturn>> resolve,
5656
Type? graphType = null,
57-
bool omitQueryArguments = false)
57+
bool omitQueryArguments = false,
58+
Func<ResolveEfFieldContext<TDbContext, TSource>, IQueryable<TReturn>, IOrderedQueryable<TReturn>>? orderBy = null)
5859
where TReturn : class =>
59-
GraphQlService.AddQueryField(this, name, resolve, graphType, omitQueryArguments);
60+
GraphQlService.AddQueryField(this, name, resolve, graphType, omitQueryArguments, orderBy);
6061

6162
public TDbContext ResolveDbContext(IResolveFieldContext<TSource> context) =>
6263
GraphQlService.ResolveDbContext(context);

src/GraphQL.EntityFramework/GraphApi/IEfGraphQLService_Queryable.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ FieldBuilder<object, TReturn> AddQueryField<TReturn>(
77
string name,
88
Func<ResolveEfFieldContext<TDbContext, object>, IQueryable<TReturn>>? resolve = null,
99
Type? itemGraphType = null,
10-
bool omitQueryArguments = false)
10+
bool omitQueryArguments = false,
11+
Func<ResolveEfFieldContext<TDbContext, object>, IQueryable<TReturn>, IOrderedQueryable<TReturn>>? orderBy = null)
1112
where TReturn : class;
1213

1314
FieldBuilder<TSource, TReturn> AddQueryField<TSource, TReturn>(
1415
IComplexGraphType graph,
1516
string name,
1617
Func<ResolveEfFieldContext<TDbContext, TSource>, IQueryable<TReturn>>? resolve = null,
1718
Type? itemGraphType = null,
18-
bool omitQueryArguments = false)
19+
bool omitQueryArguments = false,
20+
Func<ResolveEfFieldContext<TDbContext, TSource>, IQueryable<TReturn>, IOrderedQueryable<TReturn>>? orderBy = null)
1921
where TReturn : class;
2022
}

src/GraphQL.EntityFramework/GraphApi/QueryGraphType.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ public FieldBuilder<object, TReturn> AddQueryField<TReturn>(
2323
string name,
2424
Func<ResolveEfFieldContext<TDbContext, object>, IQueryable<TReturn>> resolve,
2525
Type? graphType = null,
26-
bool omitQueryArguments = false)
26+
bool omitQueryArguments = false,
27+
Func<ResolveEfFieldContext<TDbContext, object>, IQueryable<TReturn>, IOrderedQueryable<TReturn>>? orderBy = null)
2728
where TReturn : class =>
28-
GraphQlService.AddQueryField(this, name, resolve, graphType, omitQueryArguments);
29+
GraphQlService.AddQueryField(this, name, resolve, graphType, omitQueryArguments, orderBy);
2930

3031
public FieldBuilder<object, TReturn> AddSingleField<TReturn>(
3132
Func<ResolveEfFieldContext<TDbContext, object>, IQueryable<TReturn>> resolve,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
public class CustomOrderChildEntity
22
{
33
public Guid Id { get; set; } = Guid.NewGuid();
4+
public string? Property { get; set; }
45
public Guid? ParentId { get; set; }
56
public CustomOrderParentEntity? Parent { get; set; }
67
}
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
public class CustomOrderParentGraphType :
22
EfObjectGraphType<IntegrationDbContext, CustomOrderParentEntity>
33
{
4-
public CustomOrderParentGraphType(IEfGraphQLService<IntegrationDbContext> graphQlService) :
4+
public CustomOrderParentGraphType(IEfGraphQLService<IntegrationDbContext> graphQlService)
5+
:
56
base(graphQlService)
67
{
78
AddQueryField(
@@ -11,7 +12,8 @@ public CustomOrderParentGraphType(IEfGraphQLService<IntegrationDbContext> graphQ
1112
var parentId = context.Source.Id;
1213
return context.DbContext.CustomOrderChildEntities
1314
.Where(_ => _.ParentId == parentId);
14-
});
15+
},
16+
orderBy: (context, query) => query.OrderBy(_ => _.Property));
1517
AutoMap();
1618
}
1719
}

src/Tests/IntegrationTests/IntegrationTests.CustomOrder.verified.txt

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,20 @@
66
{
77
"customOrderChildren": [
88
{
9-
"id": "Guid_1"
9+
"id": "Guid_1",
10+
"property": "Value2"
1011
},
1112
{
12-
"id": "Guid_2"
13+
"id": "Guid_2",
14+
"property": "Value3"
1315
}
1416
]
1517
},
1618
{
1719
"customOrderChildren": [
1820
{
19-
"id": "Guid_3"
21+
"id": "Guid_3",
22+
"property": "Value5"
2023
}
2124
]
2225
}
@@ -32,9 +35,10 @@ FROM [CustomOrderParentEntities] AS [c],
3235
},
3336
{
3437
Text:
35-
SELECT [c].[Id], [c].[ParentId]
38+
SELECT [c].[Id], [c].[ParentId], [c].[Property]
3639
FROM [CustomOrderChildEntities] AS [c]
37-
WHERE [c].[ParentId] = @__parentId_0,
40+
WHERE [c].[ParentId] = @__parentId_0
41+
ORDER BY [c].[Property],
3842
Parameters: {
3943
@__parentId_0: {
4044
Value: Guid_4,
@@ -45,9 +49,10 @@ WHERE [c].[ParentId] = @__parentId_0,
4549
},
4650
{
4751
Text:
48-
SELECT [c].[Id], [c].[ParentId]
52+
SELECT [c].[Id], [c].[ParentId], [c].[Property]
4953
FROM [CustomOrderChildEntities] AS [c]
50-
WHERE [c].[ParentId] = @__parentId_0,
54+
WHERE [c].[ParentId] = @__parentId_0
55+
ORDER BY [c].[Property],
5156
Parameters: {
5257
@__parentId_0: {
5358
Value: Guid_5,

src/Tests/IntegrationTests/IntegrationTests.SchemaPrint.verified.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ type CustomOrderChild {
230230
parent: CustomOrderParent
231231
id: ID!
232232
parentId: ID
233+
property: String
233234
}
234235

235236
type Parent {

0 commit comments

Comments
 (0)