From 9301c487c106f590d4f60937754a6f7f12ff9fc6 Mon Sep 17 00:00:00 2001 From: Arindam Upadhyay Date: Sat, 28 Sep 2024 22:02:45 +0530 Subject: [PATCH 1/3] feat: Add BigIntegerType scalar type for GraphQL (#344) --- Lib9c.GraphQL/Types/BigIntegerType.cs | 62 +++++++++++++++++++++++++++ Mimir/Program.cs | 1 + 2 files changed, 63 insertions(+) create mode 100644 Lib9c.GraphQL/Types/BigIntegerType.cs diff --git a/Lib9c.GraphQL/Types/BigIntegerType.cs b/Lib9c.GraphQL/Types/BigIntegerType.cs new file mode 100644 index 00000000..addbe847 --- /dev/null +++ b/Lib9c.GraphQL/Types/BigIntegerType.cs @@ -0,0 +1,62 @@ +using System.Numerics; +using HotChocolate; +using HotChocolate.Language; +using HotChocolate.Types; +using System.Globalization; + +namespace Lib9c.GraphQL.Types; + +public class BigIntegerType : ScalarType +{ + public BigIntegerType() : base("BigInteger") + { + } + + public override IValueNode ParseResult(object? resultValue) + { + return resultValue switch + { + BigInteger bigInteger => ParseValue(bigInteger), + string s => ParseValue(s), + _ => throw new SerializationException( + ErrorBuilder.New() + .SetMessage("Invalid runtime type. Expected: BigInteger or string.") + .SetCode(ErrorCodes.Scalars.InvalidRuntimeType) + .Build(), + this) + }; + } + + protected override BigInteger ParseLiteral(StringValueNode valueSyntax) => + BigInteger.Parse(valueSyntax.Value, CultureInfo.InvariantCulture); + + protected override StringValueNode ParseValue(BigInteger runtimeValue) => + new(runtimeValue.ToString(CultureInfo.InvariantCulture)); + + public override bool TrySerialize(object? runtimeValue, out object? resultValue) + { + if (runtimeValue is BigInteger bigInteger) + { + resultValue = bigInteger.ToString(CultureInfo.InvariantCulture); + return true; + } + + resultValue = null; + return false; + } + + public override bool TryDeserialize(object? resultValue, out object? runtimeValue) + { + if (resultValue is string s) + { + if (BigInteger.TryParse(s, NumberStyles.Any, CultureInfo.InvariantCulture, out BigInteger bigInteger)) + { + runtimeValue = bigInteger; + return true; + } + } + + runtimeValue = null; + return false; + } +} diff --git a/Mimir/Program.cs b/Mimir/Program.cs index 7502f54e..38d51c7c 100644 --- a/Mimir/Program.cs +++ b/Mimir/Program.cs @@ -57,6 +57,7 @@ .AddLib9cGraphQLTypes() .AddMimirGraphQLTypes() .BindRuntimeType(typeof(Address), typeof(AddressType)) + .BindRuntimeType(typeof(BigIntegerType), typeof(BigIntegerType)) .BindRuntimeType(typeof(HashDigest), typeof(HashDigestSHA256Type)) .AddErrorFilter() .ModifyRequestOptions(requestExecutorOptions => { requestExecutorOptions.IncludeExceptionDetails = true; }); From 0cc48a427a44b5879e44a963f001a914efa68729 Mon Sep 17 00:00:00 2001 From: Arindam Upadhyay <79254056+Arindam2002@users.noreply.github.com> Date: Wed, 2 Oct 2024 18:49:17 +0530 Subject: [PATCH 2/3] Resolve typo in Mimir/Program.cs Co-authored-by: Lee Dogeon --- Mimir/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Mimir/Program.cs b/Mimir/Program.cs index 38d51c7c..3ca6d20e 100644 --- a/Mimir/Program.cs +++ b/Mimir/Program.cs @@ -57,7 +57,7 @@ .AddLib9cGraphQLTypes() .AddMimirGraphQLTypes() .BindRuntimeType(typeof(Address), typeof(AddressType)) - .BindRuntimeType(typeof(BigIntegerType), typeof(BigIntegerType)) + .BindRuntimeType(typeof(BigInteger), typeof(BigIntegerType)) .BindRuntimeType(typeof(HashDigest), typeof(HashDigestSHA256Type)) .AddErrorFilter() .ModifyRequestOptions(requestExecutorOptions => { requestExecutorOptions.IncludeExceptionDetails = true; }); From 0e15d866a536d694c536be9b8ae946584c58718c Mon Sep 17 00:00:00 2001 From: Arindam Upadhyay Date: Fri, 4 Oct 2024 22:13:57 +0530 Subject: [PATCH 3/3] fix: Add necessary namespace --- Mimir/Program.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Mimir/Program.cs b/Mimir/Program.cs index 3ca6d20e..471b2d70 100644 --- a/Mimir/Program.cs +++ b/Mimir/Program.cs @@ -1,3 +1,4 @@ +using System.Numerics; using System.Security.Cryptography; using System.Text.Json.Serialization; using System.Threading.RateLimiting;