Skip to content

Commit

Permalink
Sort members by default (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shane32 authored Nov 8, 2021
1 parent 2f50657 commit 1f39ada
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
13 changes: 12 additions & 1 deletion src/GraphQL.DI/AutoInputObjectGraphType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,23 @@ public AutoInputObjectGraphType()
return name;
}

/// <summary>
/// Indicates that the fields and arguments should be added to the graph type alphabetically.
/// </summary>
public static bool SortMembers { get; set; } = true;

/// <summary>
/// Returns a list of properties that should have fields created for them.
/// Sorts the list if specified by <see cref="SortMembers"/>.
/// </summary>
protected virtual IEnumerable<PropertyInfo> GetRegisteredProperties()
=> typeof(TSourceType).GetProperties(BindingFlags.Public | BindingFlags.Instance)
{
var props = typeof(TSourceType).GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(x => x.CanWrite);
if (SortMembers)
props = props.OrderBy(x => x.Name, StringComparer.InvariantCultureIgnoreCase);
return props;
}

/// <summary>
/// Processes the specified property and returns a <see cref="FieldType"/>
Expand Down
26 changes: 24 additions & 2 deletions src/GraphQL.DI/DIObjectGraphType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ public DIObjectGraphType()
/// </summary>
protected bool DefaultCreateScope { get; set; } = false;

/// <summary>
/// Indicates that the fields and arguments should be added to the graph type alphabetically.
/// </summary>
public static bool SortMembers { get; set; } = true;

/// <summary>
/// Returns a list of <see cref="DIFieldType"/> instances representing the fields ready to be
/// added to the graph type.
Expand All @@ -142,10 +147,27 @@ protected virtual List<DIFieldType> CreateFieldTypeList()
/// <summary>
/// Returns a list of methods (<see cref="MethodInfo"/> instances) on the <typeparamref name="TDIGraph"/> class to be
/// converted into field definitions.
/// Sorts the list if specified by <see cref="SortMembers"/>.
/// </summary>
protected virtual IEnumerable<MethodInfo> GetMethodsToProcess()
{
return typeof(TDIGraph).GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly).Where(x => !x.ContainsGenericParameters);
var methods = typeof(TDIGraph).GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly)
.Where(x => !x.ContainsGenericParameters);
if (SortMembers)
methods = methods.OrderBy(x => x.Name, StringComparer.InvariantCultureIgnoreCase);
return methods;
}

/// <summary>
/// Returns the list of parameters for the specified method.
/// Sorts the list if specified by <see cref="SortMembers"/>.
/// </summary>
protected IEnumerable<ParameterInfo> GetMethodParameters(MethodInfo methodInfo)
{
var parameters = methodInfo.GetParameters().AsEnumerable();
if (SortMembers)
parameters = parameters.OrderBy(x => x.Name, StringComparer.InvariantCultureIgnoreCase);
return parameters;
}

/// <summary>
Expand Down Expand Up @@ -178,7 +200,7 @@ protected virtual IEnumerable<MethodInfo> GetMethodsToProcess()
{
var resolveFieldContextParameter = Expression.Parameter(typeof(IResolveFieldContext));
var executeParams = new List<Expression>();
foreach (var param in method.GetParameters()) {
foreach (var param in GetMethodParameters(method)) {
var queryArgument = ProcessParameter(method, param, resolveFieldContextParameter, out bool isService, out Expression expr);
anyParamsUseServices |= isService;
if (queryArgument != null)
Expand Down

0 comments on commit 1f39ada

Please sign in to comment.