Skip to content

Commit 6640703

Browse files
authored
make the FormatQueryParam method virtual (#24) +semver: minor
1 parent ab97e66 commit 6640703

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

src/GraphQL.Query.Builder/QueryOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22

33
namespace GraphQL.Query.Builder
44
{

src/GraphQL.Query.Builder/QueryStringBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void Clear()
3636
/// <param name="value"></param>
3737
/// <returns>The formatted query param.</returns>
3838
/// <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)
4040
{
4141
switch (value)
4242
{
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
}

0 commit comments

Comments
 (0)