File tree Expand file tree Collapse file tree 3 files changed +62
-2
lines changed
src/GraphQL.Query.Builder
tests/GraphQL.Query.Builder.UnitTests Expand file tree Collapse file tree 3 files changed +62
-2
lines changed Original file line number Diff line number Diff line change 1
- using System ;
1
+ using System ;
2
2
3
3
namespace GraphQL . Query . Builder
4
4
{
Original file line number Diff line number Diff line change @@ -36,7 +36,7 @@ public void Clear()
36
36
/// <param name="value"></param>
37
37
/// <returns>The formatted query param.</returns>
38
38
/// <exception cref="InvalidDataException">Invalid Object Type in Param List</exception>
39
- internal protected string FormatQueryParam ( object value )
39
+ internal protected virtual string FormatQueryParam ( object value )
40
40
{
41
41
switch ( value )
42
42
{
Original file line number Diff line number Diff line change
1
+ using System ;
2
+ using System . Text ;
3
+ using Xunit ;
4
+
5
+ namespace GraphQL . Query . Builder . UnitTests
6
+ {
7
+ public class CustomQueryStringBuilderTests
8
+ {
9
+ [ Fact ]
10
+ public void TestOverride ( )
11
+ {
12
+ string query = new Query < object > ( "something" ,
13
+ new QueryOptions { QueryStringBuilderFactory = ( ) => new ConstantCaseEnumQueryStringBuilder ( ) } )
14
+ . AddArgument ( "case" , Cases . ConstantCase )
15
+ . AddField ( "some" )
16
+ . Build ( ) ;
17
+
18
+ Assert . Equal ( "something(case:CONSTANT_CASE){some}" , query ) ;
19
+ }
20
+
21
+ enum Cases
22
+ {
23
+ CamelCase ,
24
+ PascalCase ,
25
+ ConstantCase
26
+ }
27
+
28
+ class ConstantCaseEnumQueryStringBuilder : QueryStringBuilder
29
+ {
30
+ protected internal override string FormatQueryParam ( object value ) =>
31
+ value switch
32
+ {
33
+ Enum e => this . ToConstantCase ( e . ToString ( ) ) ,
34
+ _ => base . FormatQueryParam ( value )
35
+ } ;
36
+
37
+ private string ToConstantCase ( string name )
38
+ {
39
+ var sb = new StringBuilder ( ) ;
40
+ bool firstUpperLetter = true ;
41
+ foreach ( char c in name )
42
+ {
43
+ if ( char . IsUpper ( c ) )
44
+ {
45
+ if ( ! firstUpperLetter )
46
+ {
47
+ sb . Append ( "_" ) ;
48
+ }
49
+ firstUpperLetter = false ;
50
+ }
51
+
52
+ sb . Append ( char . ToUpperInvariant ( c ) ) ;
53
+ }
54
+ string result = sb . ToString ( ) ;
55
+
56
+ return result ;
57
+ }
58
+ }
59
+ }
60
+ }
You can’t perform that action at this time.
0 commit comments