11using System ;
22using System . Collections . Generic ;
3+ using System . Linq ;
34using System . Linq . Expressions ;
45using System . Reflection ;
56using System . Runtime . CompilerServices ;
67using Dawn ;
7- using Newtonsoft . Json ;
88
99[ assembly: InternalsVisibleTo ( "GraphQL.Query.Builder.UnitTests" ) ]
1010namespace GraphQL . Query . Builder
@@ -71,7 +71,7 @@ public IQuery<TSource> AddField<TProperty>(Expression<Func<TSource, TProperty>>
7171 Guard . Argument ( selector , nameof ( selector ) ) . NotNull ( ) ;
7272
7373 PropertyInfo property = GetPropertyInfo ( selector ) ;
74- string name = GetPropertyName ( property ) ;
74+ string name = PropertyNameFormatter . GetPropertyName ( property , this . options ? . Formatter ) ;
7575
7676 this . SelectList . Add ( name ) ;
7777
@@ -104,7 +104,7 @@ public IQuery<TSource> AddField<TSubSource>(
104104 Guard . Argument ( build , nameof ( build ) ) . NotNull ( ) ;
105105
106106 PropertyInfo property = GetPropertyInfo ( selector ) ;
107- string name = GetPropertyName ( property ) ;
107+ string name = PropertyNameFormatter . GetPropertyName ( property , this . options ? . Formatter ) ;
108108
109109 return AddField ( name , build ) ;
110110 }
@@ -123,7 +123,7 @@ public IQuery<TSource> AddField<TSubSource>(
123123 Guard . Argument ( build , nameof ( build ) ) . NotNull ( ) ;
124124
125125 PropertyInfo property = GetPropertyInfo ( selector ) ;
126- string name = GetPropertyName ( property ) ;
126+ string name = PropertyNameFormatter . GetPropertyName ( property , this . options ? . Formatter ) ;
127127
128128 return AddField ( name , build ) ;
129129 }
@@ -185,10 +185,16 @@ public IQuery<TSource> AddArguments<TArguments>(TArguments arguments) where TArg
185185 {
186186 Guard . Argument ( arguments , nameof ( arguments ) ) . NotNull ( ) ;
187187
188- PropertyInfo [ ] properties = typeof ( TArguments ) . GetProperties ( ) ;
188+ IEnumerable < PropertyInfo > properties = arguments
189+ . GetType ( )
190+ . GetProperties ( )
191+ . Where ( property => property . GetValue ( arguments ) != null )
192+ . OrderBy ( property => property . Name ) ;
189193 foreach ( PropertyInfo property in properties )
190194 {
191- this . Arguments . Add ( this . GetPropertyName ( property ) , property . GetValue ( arguments ) ) ;
195+ this . Arguments . Add (
196+ PropertyNameFormatter . GetPropertyName ( property , this . options ? . Formatter ) ,
197+ property . GetValue ( arguments ) ) ;
192198 }
193199
194200 return this ;
@@ -238,30 +244,5 @@ private static PropertyInfo GetPropertyInfo<TProperty>(Expression<Func<TSource,
238244
239245 return propertyInfo ;
240246 }
241-
242- /// <summary>Tries to get property name from JSON property attribute or from optional formater.</summary>
243- /// <param name="property">The property.</param>
244- /// <returns>The property name.</returns>
245- private string GetPropertyName ( PropertyInfo property )
246- {
247- Guard . Argument ( property , nameof ( property ) ) . NotNull ( ) ;
248-
249- Attribute attribute = property . GetCustomAttribute ( typeof ( JsonPropertyAttribute ) ) ;
250-
251- if ( attribute != null )
252- {
253- if ( ! string . IsNullOrEmpty ( ( attribute as JsonPropertyAttribute ) . PropertyName ) )
254- {
255- return ( attribute as JsonPropertyAttribute ) . PropertyName ;
256- }
257- }
258-
259- if ( this . options ? . Formatter != null )
260- {
261- return this . options . Formatter . Invoke ( property . Name ) ;
262- }
263-
264- return property . Name ;
265- }
266247 }
267248}
0 commit comments