Skip to content

Commit

Permalink
add orderby callback
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Oct 29, 2024
1 parent f686c16 commit e6a2dec
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project>
<PropertyGroup>
<NoWarn>CS1591;NU5104;CS1573;CS9107</NoWarn>
<Version>27.2.2</Version>
<Version>28.0.0-beta.1</Version>
<LangVersion>preview</LangVersion>
<AssemblyVersion>1.0.0</AssemblyVersion>
<PackageTags>EntityFrameworkCore, EntityFramework, GraphQL</PackageTags>
Expand Down
18 changes: 13 additions & 5 deletions src/GraphQL.EntityFramework/GraphApi/EfGraphQLService_Queryable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ public FieldBuilder<object, TReturn> AddQueryField<TReturn>(
string name,
Func<ResolveEfFieldContext<TDbContext, object>, IQueryable<TReturn>>? resolve = null,
Type? graphType = null,
bool omitQueryArguments = false)
bool omitQueryArguments = false,
Func<ResolveEfFieldContext<TDbContext, object>, IQueryable<TReturn>, IOrderedQueryable<TReturn>>? orderBy = null)
where TReturn : class
{
var field = BuildQueryField(graphType, name, resolve, omitQueryArguments);
var field = BuildQueryField(graphType, name, resolve, omitQueryArguments, orderBy);
graph.AddField(field);
return new FieldBuilderEx<object, TReturn>(field);
}
Expand All @@ -21,10 +22,11 @@ public FieldBuilder<TSource, TReturn> AddQueryField<TSource, TReturn>(
string name,
Func<ResolveEfFieldContext<TDbContext, TSource>, IQueryable<TReturn>>? resolve = null,
Type? itemGraphType = null,
bool omitQueryArguments = false)
bool omitQueryArguments = false,
Func<ResolveEfFieldContext<TDbContext, TSource>, IQueryable<TReturn>, IOrderedQueryable<TReturn>>? orderBy = null)
where TReturn : class
{
var field = BuildQueryField(itemGraphType, name, resolve, omitQueryArguments);
var field = BuildQueryField(itemGraphType, name, resolve, omitQueryArguments, orderBy);
graph.AddField(field);
return new FieldBuilderEx<TSource, TReturn>(field);
}
Expand All @@ -33,7 +35,8 @@ FieldType BuildQueryField<TSource, TReturn>(
Type? itemGraphType,
string name,
Func<ResolveEfFieldContext<TDbContext, TSource>, IQueryable<TReturn>>? resolve,
bool omitQueryArguments)
bool omitQueryArguments,
Func<ResolveEfFieldContext<TDbContext, TSource>, IQueryable<TReturn>, IOrderedQueryable<TReturn>>? orderBy)
where TReturn : class
{
Guard.AgainstWhiteSpace(nameof(name), name);
Expand Down Expand Up @@ -65,6 +68,11 @@ FieldType BuildQueryField<TSource, TReturn>(
query = query.ApplyGraphQlArguments(context, names, true, omitQueryArguments);
}
if(orderBy is not null)
{
query = orderBy(fieldContext, query);
}
QueryLogger.Write(query);
List<TReturn> list;
Expand Down
5 changes: 3 additions & 2 deletions src/GraphQL.EntityFramework/GraphApi/EfInterfaceGraphType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ public ConnectionBuilder<TSource> AddQueryConnectionField<TReturn>(
public FieldBuilder<object, TReturn> AddQueryField<TReturn>(
string name,
Type? graphType = null,
bool omitQueryArguments = false)
bool omitQueryArguments = false,
Func<ResolveEfFieldContext<TDbContext, object>, IQueryable<TReturn>, IOrderedQueryable<TReturn>>? orderBy = null)
where TReturn : class =>
GraphQlService.AddQueryField<TReturn>(this, name, null, graphType, omitQueryArguments);
GraphQlService.AddQueryField(this, name, null, graphType, omitQueryArguments, orderBy);

public TDbContext ResolveDbContext(IResolveFieldContext<TSource> context) =>
GraphQlService.ResolveDbContext(context);
Expand Down
5 changes: 3 additions & 2 deletions src/GraphQL.EntityFramework/GraphApi/EfObjectGraphType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ public FieldBuilder<TSource, TReturn> AddQueryField<TReturn>(
string name,
Func<ResolveEfFieldContext<TDbContext, TSource>, IQueryable<TReturn>> resolve,
Type? graphType = null,
bool omitQueryArguments = false)
bool omitQueryArguments = false,
Func<ResolveEfFieldContext<TDbContext, TSource>, IQueryable<TReturn>, IOrderedQueryable<TReturn>>? orderBy = null)
where TReturn : class =>
GraphQlService.AddQueryField(this, name, resolve, graphType, omitQueryArguments);
GraphQlService.AddQueryField(this, name, resolve, graphType, omitQueryArguments, orderBy);

public TDbContext ResolveDbContext(IResolveFieldContext<TSource> context) =>
GraphQlService.ResolveDbContext(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ FieldBuilder<object, TReturn> AddQueryField<TReturn>(
string name,
Func<ResolveEfFieldContext<TDbContext, object>, IQueryable<TReturn>>? resolve = null,
Type? itemGraphType = null,
bool omitQueryArguments = false)
bool omitQueryArguments = false,
Func<ResolveEfFieldContext<TDbContext, object>, IQueryable<TReturn>, IOrderedQueryable<TReturn>>? orderBy = null)
where TReturn : class;

FieldBuilder<TSource, TReturn> AddQueryField<TSource, TReturn>(
IComplexGraphType graph,
string name,
Func<ResolveEfFieldContext<TDbContext, TSource>, IQueryable<TReturn>>? resolve = null,
Type? itemGraphType = null,
bool omitQueryArguments = false)
bool omitQueryArguments = false,
Func<ResolveEfFieldContext<TDbContext, TSource>, IQueryable<TReturn>, IOrderedQueryable<TReturn>>? orderBy = null)
where TReturn : class;
}
5 changes: 3 additions & 2 deletions src/GraphQL.EntityFramework/GraphApi/QueryGraphType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ public FieldBuilder<object, TReturn> AddQueryField<TReturn>(
string name,
Func<ResolveEfFieldContext<TDbContext, object>, IQueryable<TReturn>> resolve,
Type? graphType = null,
bool omitQueryArguments = false)
bool omitQueryArguments = false,
Func<ResolveEfFieldContext<TDbContext, object>, IQueryable<TReturn>, IOrderedQueryable<TReturn>>? orderBy = null)
where TReturn : class =>
GraphQlService.AddQueryField(this, name, resolve, graphType, omitQueryArguments);
GraphQlService.AddQueryField(this, name, resolve, graphType, omitQueryArguments, orderBy);

public FieldBuilder<object, TReturn> AddSingleField<TReturn>(
Func<ResolveEfFieldContext<TDbContext, object>, IQueryable<TReturn>> resolve,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
public class CustomOrderChildEntity
{
public Guid Id { get; set; } = Guid.NewGuid();
public string? Property { get; set; }
public Guid? ParentId { get; set; }
public CustomOrderParentEntity? Parent { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
public class CustomOrderParentGraphType :
EfObjectGraphType<IntegrationDbContext, CustomOrderParentEntity>
{
public CustomOrderParentGraphType(IEfGraphQLService<IntegrationDbContext> graphQlService) :
public CustomOrderParentGraphType(IEfGraphQLService<IntegrationDbContext> graphQlService)
:
base(graphQlService)
{
AddQueryField(
Expand All @@ -11,7 +12,8 @@ public CustomOrderParentGraphType(IEfGraphQLService<IntegrationDbContext> graphQ
var parentId = context.Source.Id;
return context.DbContext.CustomOrderChildEntities
.Where(_ => _.ParentId == parentId);
});
},
orderBy: (context, query) => query.OrderBy(_ => _.Property));
AutoMap();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@
{
"customOrderChildren": [
{
"id": "Guid_1"
"id": "Guid_1",
"property": "Value2"
},
{
"id": "Guid_2"
"id": "Guid_2",
"property": "Value3"
}
]
},
{
"customOrderChildren": [
{
"id": "Guid_3"
"id": "Guid_3",
"property": "Value5"
}
]
}
Expand All @@ -32,9 +35,10 @@ FROM [CustomOrderParentEntities] AS [c],
},
{
Text:
SELECT [c].[Id], [c].[ParentId]
SELECT [c].[Id], [c].[ParentId], [c].[Property]
FROM [CustomOrderChildEntities] AS [c]
WHERE [c].[ParentId] = @__parentId_0,
WHERE [c].[ParentId] = @__parentId_0
ORDER BY [c].[Property],
Parameters: {
@__parentId_0: {
Value: Guid_4,
Expand All @@ -45,9 +49,10 @@ WHERE [c].[ParentId] = @__parentId_0,
},
{
Text:
SELECT [c].[Id], [c].[ParentId]
SELECT [c].[Id], [c].[ParentId], [c].[Property]
FROM [CustomOrderChildEntities] AS [c]
WHERE [c].[ParentId] = @__parentId_0,
WHERE [c].[ParentId] = @__parentId_0
ORDER BY [c].[Property],
Parameters: {
@__parentId_0: {
Value: Guid_5,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ type CustomOrderChild {
parent: CustomOrderParent
id: ID!
parentId: ID
property: String
}

type Parent {
Expand Down
12 changes: 8 additions & 4 deletions src/Tests/IntegrationTests/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2524,7 +2524,8 @@ public async Task CustomOrder()
{
customOrderChildren
{
id
id,
property
}
}
}
Expand All @@ -2533,18 +2534,21 @@ public async Task CustomOrder()
var entity1 = new CustomOrderParentEntity();
var entity2 = new CustomOrderChildEntity
{
Parent = entity1
Parent = entity1,
Property = "Value2"
};
var entity3 = new CustomOrderChildEntity
{
Parent = entity1
Parent = entity1,
Property = "Value3"
};
entity1.Children.Add(entity2);
entity1.Children.Add(entity3);
var entity4 = new CustomOrderParentEntity();
var entity5 = new CustomOrderChildEntity
{
Parent = entity4
Parent = entity4,
Property = "Value5"
};
entity4.Children.Add(entity5);

Expand Down

0 comments on commit e6a2dec

Please sign in to comment.