Skip to content

Commit

Permalink
Roll back exposed GraphQL API changes by adding LegacyBencodexValueType
Browse files Browse the repository at this point in the history
  • Loading branch information
greymistcube committed Jan 2, 2024
1 parent 1b34825 commit f12184e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 27 deletions.
31 changes: 5 additions & 26 deletions Libplanet.Explorer.Tests/Queries/StateQueryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ public async Task States()
addresses: [""0x5003712B63baAB98094aD678EA2B24BcE445D076"", ""0x0000000000000000000000000000000000000000""],
offsetBlockHash:
""01ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b""
) {
hex
}
)
}
", source: source);
Assert.Null(result.Errors);
Expand All @@ -45,14 +43,7 @@ public async Task States()
object[] states =
Assert.IsAssignableFrom<object[]>(resultDict["states"]);
Assert.Equal(2, states.Length);
Assert.Equal(
new Dictionary<string, object>()
{
{ "hex", "6e" },
},
Assert.IsAssignableFrom<IDictionary<string, object>>(states[0])
);
Assert.Null(states[1]);
Assert.Equal(new[] { new byte[] { 110, }, null }, states);
}

[Fact]
Expand Down Expand Up @@ -186,9 +177,7 @@ public async Task ThrowExecutionErrorIfViolateMutualExclusive()
""01ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b"",
offsetStateRootHash:
""c33b27773104f75ac9df5b0533854108bd498fab31e5236b6f1e1f6404d5ef64""
) {
hex
}
)
}
", source: source);
Assert.IsType<ExecutionErrors>(result.Errors);
Expand All @@ -205,9 +194,7 @@ public async Task StatesBySrh()
addresses: [""0x5003712B63baAB98094aD678EA2B24BcE445D076"", ""0x0000000000000000000000000000000000000000""],
offsetStateRootHash:
""c33b27773104f75ac9df5b0533854108bd498fab31e5236b6f1e1f6404d5ef64""
) {
hex
}
)
}
", source: source);
Assert.Null(result.Errors);
Expand All @@ -216,15 +203,7 @@ public async Task StatesBySrh()
Assert.IsAssignableFrom<IDictionary<string, object>>(resultData!.ToValue());
object[] states =
Assert.IsAssignableFrom<object[]>(resultDict["states"]);
Assert.Equal(2, states.Length);
Assert.Equal(
new Dictionary<string, object>()
{
{ "hex", "6e" },
},
Assert.IsAssignableFrom<IDictionary<string, object>>(states[0])
);
Assert.Null(states[1]);
Assert.Equal(new[] { new byte[] { 110, }, null }, states);
}

[Fact]
Expand Down
51 changes: 51 additions & 0 deletions Libplanet.Explorer/GraphTypes/LegacyBencodexValueType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using Bencodex;
using GraphQL.Language.AST;
using GraphQL.Types;
using Libplanet.Common;

namespace Libplanet.Explorer.GraphTypes
{
public class LegacyBencodexValueType : StringGraphType
{
private static readonly Codec _codec = new();

public LegacyBencodexValueType()
{
Name = "BencodexValue";
}

public override object? Serialize(object? value)
{
if (value is Bencodex.Types.IValue iv)
{
return _codec.Encode(iv);
}

return value;
}

public override object? ParseValue(object? value)
{
return value switch
{
null => null,
string hex => _codec.Decode(ByteUtil.ParseHex(hex)),
_ => throw new ArgumentException(
$"Expected a hexadecimal string but {value}",
nameof(value)
),
};
}

public override object? ParseLiteral(IValue? value)
{
if (value is StringValue)
{
return ParseValue(value.Value);
}

return null;
}
}
}
2 changes: 1 addition & 1 deletion Libplanet.Explorer/Queries/StateQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class StateQuery : ObjectGraphType<IBlockChainStates>
public StateQuery()
{
Name = "StateQuery";
Field<NonNullGraphType<ListGraphType<BencodexValueType>>>(
Field<NonNullGraphType<ListGraphType<LegacyBencodexValueType>>>(
"states",
arguments: new QueryArguments(
new QueryArgument<NonNullGraphType<ListGraphType<NonNullGraphType<AddressType>>>>
Expand Down

0 comments on commit f12184e

Please sign in to comment.