Skip to content

Commit a0d1dfc

Browse files
authored
Merge pull request #38 from Linq2GraphQL/add-support-custom-scalars
Add support custom scalars
2 parents a685c43 + ab952c1 commit a0d1dfc

File tree

23 files changed

+693
-33
lines changed

23 files changed

+693
-33
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System.Text.Json.Serialization;
2+
using System.Text.Json;
3+
4+
5+
namespace Linq2GraphQL.Client
6+
{
7+
public class CustomScalar
8+
{
9+
public string Value { get; set; }
10+
11+
public override string ToString() => Value;
12+
}
13+
14+
public class CustomScalarConverter<TScalar> : JsonConverter<TScalar>
15+
where TScalar : CustomScalar, new()
16+
{
17+
public override TScalar Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
18+
{
19+
var value = reader.GetString();
20+
if (value is null)
21+
{
22+
return null;
23+
}
24+
25+
var scalar = new TScalar
26+
{
27+
Value = value
28+
};
29+
30+
return scalar;
31+
}
32+
33+
public override void Write(Utf8JsonWriter writer, TScalar value, JsonSerializerOptions options)
34+
{
35+
writer.WriteStringValue(value.Value);
36+
}
37+
38+
}
39+
}

src/Linq2GraphQL.Client/Converters/EnumConverter.cs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,6 @@
66

77
namespace Linq2GraphQL.Client;
88

9-
//internal static class Program
10-
//{
11-
// public static void Main(string[] args)
12-
// {
13-
// string json = JsonSerializer.Serialize(FlagDefinitions.One | FlagDefinitions.Two);
14-
// FlagDefinitions flags = JsonSerializer.Deserialize<FlagDefinitions>(json);
15-
// }
16-
//}
17-
18-
//[JsonConverter(typeof(JsonStringEnumMemberConverter))]
19-
//[Flags]
20-
//public enum FlagDefinitions
21-
//{
22-
// None = 0x00,
23-
24-
// [EnumMember(Value = "all values")]
25-
// All = One | Two | Three | Four,
26-
27-
// [EnumMember(Value = "one value")]
28-
// One = 0x01,
29-
// [EnumMember(Value = "two value")]
30-
// Two = 0x02,
31-
// [EnumMember(Value = "three value")]
32-
// Three = 0x04,
33-
// [EnumMember(Value = "four value")]
34-
// Four = 0x08,
35-
//}
369

3710
public class JsonStringEnumMemberConverter : JsonConverterFactory
3811
{

src/Linq2GraphQL.Client/Extensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ internal static bool IsValueTypeOrString(this Type type)
1212
return false;
1313
}
1414

15-
return type.IsValueType || type == typeof(string);
15+
return type.IsValueType || type == typeof(string) || type.IsSubclassOf(typeof(CustomScalar)); ;
1616
}
1717

1818
internal static bool IsListOfPrimitiveTypeOrString(this Type type)

src/Linq2GraphQL.Generator/ClientGenerator.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Linq2GraphQL.Generator.Templates.Enum;
44
using Linq2GraphQL.Generator.Templates.Interface;
55
using Linq2GraphQL.Generator.Templates.Methods;
6+
using Linq2GraphQL.Generator.Templates.Scalars;
67
using System.Net.Http.Headers;
78
using System.Net.Http.Json;
89
using System.Text.Json;
@@ -145,7 +146,16 @@ public List<FileEntry> Generate(string schemaJson)
145146
var includeQuery = queryType != null;
146147
var includeMutation = mutationType != null;
147148

148-
Console.WriteLine("Generate Client...");
149+
Console.WriteLine("Generate Scalars...");
150+
foreach (var scalar in schema.GetCustomScalars())
151+
{
152+
var scalarText = new ScalarTemplate(scalar, namespaceName).TransformText();
153+
AddFile("Scalars", scalar.FileName, scalarText);
154+
155+
}
156+
157+
158+
Console.WriteLine("Generate Client...");
149159
var templateText = new ClientTemplate(namespaceName, clientName, queryType, mutationType, subscriptionType)
150160
.TransformText();
151161
var fileName = clientName + ".cs";

src/Linq2GraphQL.Generator/GraphQLSchema/Schema.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ public List<GraphqlType> GetEnums()
6565
return GetAllTypesExceptSystemTypes().Where(e => e.Kind == TypeKind.Enum).ToList();
6666
}
6767

68+
public List<GraphqlType> GetCustomScalars()
69+
{
70+
var mappers = Helpers.TypeMapping;
71+
return GetAllTypesExceptSystemTypes().Where(e => e.Kind == TypeKind.Scalar && !mappers.ContainsKey(e.Name)).ToList();
72+
}
73+
6874
public List<GraphqlType> GetInterfaces()
6975
{
7076
return GetAllTypesExceptSystemTypes().Where(e => e.Kind == TypeKind.Interface).ToList();

src/Linq2GraphQL.Generator/Linq2GraphQL.Generator.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@
5858
<Generator>TextTemplatingFilePreprocessor</Generator>
5959
<LastGenOutput>ClientExtensionsTemplate.cs</LastGenOutput>
6060
</None>
61+
<None Update="Templates\Scalars\ScalarTemplate.tt">
62+
<Generator>TextTemplatingFilePreprocessor</Generator>
63+
<LastGenOutput>ScalarTemplate.cs</LastGenOutput>
64+
</None>
6165
</ItemGroup>
6266

6367
<ItemGroup>
@@ -105,6 +109,11 @@
105109
<DesignTime>True</DesignTime>
106110
<DependentUpon>ClientExtensionsTemplate.tt</DependentUpon>
107111
</Compile>
112+
<Compile Update="Templates\Scalars\ScalarTemplate.cs">
113+
<DesignTime>True</DesignTime>
114+
<AutoGen>True</AutoGen>
115+
<DependentUpon>ScalarTemplate.tt</DependentUpon>
116+
</Compile>
108117
</ItemGroup>
109118

110119
<ItemGroup>

0 commit comments

Comments
 (0)