1
1
using System ;
2
2
using System . Collections . Generic ;
3
+ using System . Linq ;
3
4
using System . Linq . Expressions ;
4
5
using System . Reflection ;
5
6
using System . Runtime . CompilerServices ;
6
7
using Dawn ;
7
- using Newtonsoft . Json ;
8
8
9
9
[ assembly: InternalsVisibleTo ( "GraphQL.Query.Builder.UnitTests" ) ]
10
10
namespace GraphQL . Query . Builder
@@ -71,7 +71,7 @@ public IQuery<TSource> AddField<TProperty>(Expression<Func<TSource, TProperty>>
71
71
Guard . Argument ( selector , nameof ( selector ) ) . NotNull ( ) ;
72
72
73
73
PropertyInfo property = GetPropertyInfo ( selector ) ;
74
- string name = GetPropertyName ( property ) ;
74
+ string name = PropertyNameFormatter . GetPropertyName ( property , this . options ? . Formatter ) ;
75
75
76
76
this . SelectList . Add ( name ) ;
77
77
@@ -104,7 +104,7 @@ public IQuery<TSource> AddField<TSubSource>(
104
104
Guard . Argument ( build , nameof ( build ) ) . NotNull ( ) ;
105
105
106
106
PropertyInfo property = GetPropertyInfo ( selector ) ;
107
- string name = GetPropertyName ( property ) ;
107
+ string name = PropertyNameFormatter . GetPropertyName ( property , this . options ? . Formatter ) ;
108
108
109
109
return AddField ( name , build ) ;
110
110
}
@@ -123,7 +123,7 @@ public IQuery<TSource> AddField<TSubSource>(
123
123
Guard . Argument ( build , nameof ( build ) ) . NotNull ( ) ;
124
124
125
125
PropertyInfo property = GetPropertyInfo ( selector ) ;
126
- string name = GetPropertyName ( property ) ;
126
+ string name = PropertyNameFormatter . GetPropertyName ( property , this . options ? . Formatter ) ;
127
127
128
128
return AddField ( name , build ) ;
129
129
}
@@ -185,10 +185,16 @@ public IQuery<TSource> AddArguments<TArguments>(TArguments arguments) where TArg
185
185
{
186
186
Guard . Argument ( arguments , nameof ( arguments ) ) . NotNull ( ) ;
187
187
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 ) ;
189
193
foreach ( PropertyInfo property in properties )
190
194
{
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 ) ) ;
192
198
}
193
199
194
200
return this ;
@@ -238,30 +244,5 @@ private static PropertyInfo GetPropertyInfo<TProperty>(Expression<Func<TSource,
238
244
239
245
return propertyInfo ;
240
246
}
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
- }
266
247
}
267
248
}
0 commit comments