Skip to content

Commit

Permalink
Merge pull request #38 from Linq2GraphQL/add-support-custom-scalars
Browse files Browse the repository at this point in the history
Add support custom scalars
  • Loading branch information
joadan authored Sep 5, 2024
2 parents a685c43 + ab952c1 commit a0d1dfc
Show file tree
Hide file tree
Showing 23 changed files with 693 additions and 33 deletions.
39 changes: 39 additions & 0 deletions src/Linq2GraphQL.Client/Converters/CustomScalarConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Text.Json.Serialization;
using System.Text.Json;


namespace Linq2GraphQL.Client
{
public class CustomScalar
{
public string Value { get; set; }

public override string ToString() => Value;
}

public class CustomScalarConverter<TScalar> : JsonConverter<TScalar>
where TScalar : CustomScalar, new()
{
public override TScalar Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var value = reader.GetString();
if (value is null)
{
return null;
}

var scalar = new TScalar
{
Value = value
};

return scalar;
}

public override void Write(Utf8JsonWriter writer, TScalar value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.Value);
}

}
}
27 changes: 0 additions & 27 deletions src/Linq2GraphQL.Client/Converters/EnumConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,6 @@

namespace Linq2GraphQL.Client;

//internal static class Program
//{
// public static void Main(string[] args)
// {
// string json = JsonSerializer.Serialize(FlagDefinitions.One | FlagDefinitions.Two);
// FlagDefinitions flags = JsonSerializer.Deserialize<FlagDefinitions>(json);
// }
//}

//[JsonConverter(typeof(JsonStringEnumMemberConverter))]
//[Flags]
//public enum FlagDefinitions
//{
// None = 0x00,

// [EnumMember(Value = "all values")]
// All = One | Two | Three | Four,

// [EnumMember(Value = "one value")]
// One = 0x01,
// [EnumMember(Value = "two value")]
// Two = 0x02,
// [EnumMember(Value = "three value")]
// Three = 0x04,
// [EnumMember(Value = "four value")]
// Four = 0x08,
//}

public class JsonStringEnumMemberConverter : JsonConverterFactory
{
Expand Down
2 changes: 1 addition & 1 deletion src/Linq2GraphQL.Client/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal static bool IsValueTypeOrString(this Type type)
return false;
}

return type.IsValueType || type == typeof(string);
return type.IsValueType || type == typeof(string) || type.IsSubclassOf(typeof(CustomScalar)); ;
}

internal static bool IsListOfPrimitiveTypeOrString(this Type type)
Expand Down
12 changes: 11 additions & 1 deletion src/Linq2GraphQL.Generator/ClientGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Linq2GraphQL.Generator.Templates.Enum;
using Linq2GraphQL.Generator.Templates.Interface;
using Linq2GraphQL.Generator.Templates.Methods;
using Linq2GraphQL.Generator.Templates.Scalars;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text.Json;
Expand Down Expand Up @@ -145,7 +146,16 @@ public List<FileEntry> Generate(string schemaJson)
var includeQuery = queryType != null;
var includeMutation = mutationType != null;

Console.WriteLine("Generate Client...");
Console.WriteLine("Generate Scalars...");
foreach (var scalar in schema.GetCustomScalars())
{
var scalarText = new ScalarTemplate(scalar, namespaceName).TransformText();
AddFile("Scalars", scalar.FileName, scalarText);

}


Console.WriteLine("Generate Client...");
var templateText = new ClientTemplate(namespaceName, clientName, queryType, mutationType, subscriptionType)
.TransformText();
var fileName = clientName + ".cs";
Expand Down
6 changes: 6 additions & 0 deletions src/Linq2GraphQL.Generator/GraphQLSchema/Schema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ public List<GraphqlType> GetEnums()
return GetAllTypesExceptSystemTypes().Where(e => e.Kind == TypeKind.Enum).ToList();
}

public List<GraphqlType> GetCustomScalars()
{
var mappers = Helpers.TypeMapping;
return GetAllTypesExceptSystemTypes().Where(e => e.Kind == TypeKind.Scalar && !mappers.ContainsKey(e.Name)).ToList();
}

public List<GraphqlType> GetInterfaces()
{
return GetAllTypesExceptSystemTypes().Where(e => e.Kind == TypeKind.Interface).ToList();
Expand Down
9 changes: 9 additions & 0 deletions src/Linq2GraphQL.Generator/Linq2GraphQL.Generator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@
<Generator>TextTemplatingFilePreprocessor</Generator>
<LastGenOutput>ClientExtensionsTemplate.cs</LastGenOutput>
</None>
<None Update="Templates\Scalars\ScalarTemplate.tt">
<Generator>TextTemplatingFilePreprocessor</Generator>
<LastGenOutput>ScalarTemplate.cs</LastGenOutput>
</None>
</ItemGroup>

<ItemGroup>
Expand Down Expand Up @@ -105,6 +109,11 @@
<DesignTime>True</DesignTime>
<DependentUpon>ClientExtensionsTemplate.tt</DependentUpon>
</Compile>
<Compile Update="Templates\Scalars\ScalarTemplate.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>ScalarTemplate.tt</DependentUpon>
</Compile>
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit a0d1dfc

Please sign in to comment.