Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BigIntegerType to GraphQL Types #350

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions Lib9c.GraphQL/Types/BigIntegerType.cs
Original file line number Diff line number Diff line change
@@ -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<BigInteger, StringValueNode>
{
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;
}
}
1 change: 1 addition & 0 deletions Mimir/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
.AddLib9cGraphQLTypes()
.AddMimirGraphQLTypes()
.BindRuntimeType(typeof(Address), typeof(AddressType))
.BindRuntimeType(typeof(BigIntegerType), typeof(BigIntegerType))
.BindRuntimeType(typeof(HashDigest<SHA256>), typeof(HashDigestSHA256Type))
.AddErrorFilter<ErrorFilter>()
.ModifyRequestOptions(requestExecutorOptions => { requestExecutorOptions.IncludeExceptionDetails = true; });
Expand Down