Skip to content

Commit dcbc4f9

Browse files
authored
Merge pull request #39 from Linq2GraphQL/add-support-custom-scalars
More Scalars
2 parents a0d1dfc + af1ce3a commit dcbc4f9

File tree

4 files changed

+35
-22
lines changed

4 files changed

+35
-22
lines changed

src/Linq2GraphQL.Client/Converters/CustomScalarConverter.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
11
using System.Text.Json.Serialization;
22
using System.Text.Json;
3+
using Linq2GraphQL.Client;
34

45

56
namespace Linq2GraphQL.Client
67
{
78
public class CustomScalar
89
{
9-
public string Value { get; set; }
10+
internal string InternalValue { get; set; }
1011

11-
public override string ToString() => Value;
12+
13+
public virtual string Value
14+
{
15+
get { return InternalValue; }
16+
17+
set { InternalValue = value; }
18+
19+
}
1220
}
1321

22+
23+
1424
public class CustomScalarConverter<TScalar> : JsonConverter<TScalar>
1525
where TScalar : CustomScalar, new()
1626
{
@@ -24,15 +34,15 @@ public override TScalar Read(ref Utf8JsonReader reader, Type typeToConvert, Json
2434

2535
var scalar = new TScalar
2636
{
27-
Value = value
37+
InternalValue = value
2838
};
2939

3040
return scalar;
3141
}
3242

3343
public override void Write(Utf8JsonWriter writer, TScalar value, JsonSerializerOptions options)
3444
{
35-
writer.WriteStringValue(value.Value);
45+
writer.WriteStringValue(value.InternalValue);
3646
}
3747

3848
}

test/Linq2GraphQL.TestClientNullable/ExtendedScalars/MacAddress.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,23 @@ namespace Linq2GraphQL.TestClientNullable
99
public partial class MacAddress
1010
{
1111

12+
public MacAddress() { }
1213

13-
public string GetValue()
14+
public MacAddress(string value)
1415
{
15-
16-
return "Value: " + Value;
17-
16+
Value = value;
1817
}
1918

20-
19+
public override string Value
20+
{
21+
get {
22+
//Customer Code
23+
return base.Value; }
24+
set {
25+
//Custom code
26+
27+
base.Value = value; }
28+
}
2129

2230

2331

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11

22
using Linq2GraphQL.Client;
3-
using System.Reflection.Metadata;
43
using System.Text.Json.Serialization;
54

65
namespace Linq2GraphQL.TestClientNullable;
76

8-
/// <summary>
9-
/// The `MacAddress` scalar type represents a IEEE 802 48-bit or 64-bit Mac address, represented as UTF-8 character sequences. The scalar follows the specifications defined in RFC7042 and RFC7043 respectively.
10-
/// </summary>
11-
[JsonConverter(typeof(CustomScalarConverter<MacAddress>))]
12-
public partial class MacAddress : CustomScalar
13-
{
14-
15-
16-
}
7+
/// <summary>
8+
/// The `MacAddress` scalar type represents a IEEE 802 48-bit or 64-bit Mac address, represented as UTF-8 character sequences. The scalar follows the specifications defined in RFC7042 and RFC7043 respectively.
9+
/// </summary>
10+
[JsonConverter(typeof(CustomScalarConverter<MacAddress>))]
11+
public partial class MacAddress : CustomScalar {}

test/Linq2GraphQL.Tests/ScalarTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ public async Task SetScalar()
3535
{
3636
var result = await sampleClient
3737
.Mutation
38-
.UpdatePerson(person: new PersonInput { Name = "Peter", MacAddress= new MacAddress { Value = "01-23-45-67-89-ab" } })
38+
.UpdatePerson(person: new PersonInput { Name = "Peter", MacAddress = new MacAddress("01-23-45-67-89-ab") })
3939
.Select()
4040
.ExecuteAsync();
4141

42-
var macAddress = result.MacAddress;
4342

44-
Assert.NotNull(macAddress);
43+
44+
Assert.NotNull(result.MacAddress);
4545
}
4646

4747

0 commit comments

Comments
 (0)