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

✨ QoL GraphQL queries #3561

Merged
Merged
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
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ To be released.
`ObjectGraphType<IValue>` instead of `StringGraphType`. Instead of
simply being a hexadecimal representation of `byte[]` encoded `IValue`,
now one can choose its representation format. [[#3560]]
- (Libplanet.Explorer) Added `HelperQuery`, a collection of utility like
queries. [[#3561]]

[#3559]: https://github.com/planetarium/libplanet/pull/3559
[#3560]: https://github.com/planetarium/libplanet/pull/3560
[#3561]: https://github.com/planetarium/libplanet/pull/3561


Version 3.9.2
Expand Down
63 changes: 63 additions & 0 deletions Libplanet.Explorer.Tests/Queries/HelperQueryTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using GraphQL;
using GraphQL.Execution;
using Libplanet.Common;
using static Libplanet.Explorer.Tests.GraphQLTestUtils;
using Libplanet.Explorer.Queries;
using Libplanet.Types.Assets;
using Xunit;

namespace Libplanet.Explorer.Tests.Queries;

public class HelperQueryTest
{
[Fact]
public async Task CurrencyHash()
{
Currency currency = Currency.Uncapped("ABC", 2, minters: null);

ExecutionResult result = await ExecuteQueryAsync<HelperQuery>(@"
{
currencyHash (
currency: { ticker: ""ABC"", decimalPlaces: 2, totalSupplyTrackable: true }
)
}
");

Assert.Null(result.Errors);
ExecutionNode resultData = Assert.IsAssignableFrom<ExecutionNode>(result.Data);
IDictionary<string, object> resultDict =
Assert.IsAssignableFrom<IDictionary<string, object>>(resultData!.ToValue());
string hash =
Assert.IsAssignableFrom<string>(resultDict["currencyHash"]);
Assert.Equal(ByteUtil.Hex(currency.Hash.ByteArray), hash);
}

[Fact]
public async Task BencodexValue()
{
ExecutionResult result = await ExecuteQueryAsync<HelperQuery>(@"
{
bencodexValue (hex: ""7531323a68656c6c6f20776f726c6421"") {
hex
json
inspection
}
}
");

Assert.Null(result.Errors);
ExecutionNode resultData = Assert.IsAssignableFrom<ExecutionNode>(result.Data);
IDictionary<string, object> resultDict =
Assert.IsAssignableFrom<IDictionary<string, object>>(resultData!.ToValue());
IDictionary<string, object> bencodexValue =
Assert.IsAssignableFrom<IDictionary<string, object>>(resultDict["bencodexValue"]);
string hex = Assert.IsAssignableFrom<string>(bencodexValue["hex"]);
string json = Assert.IsAssignableFrom<string>(bencodexValue["json"]);
string inspection = Assert.IsAssignableFrom<string>(bencodexValue["inspection"]);
Assert.Equal("7531323a68656c6c6f20776f726c6421", hex);
Assert.Equal("\"\\uFEFFhello world!\"", json);
Assert.Equal("\"hello world!\"", inspection);
}
}
1 change: 1 addition & 0 deletions Libplanet.Explorer/Queries/ExplorerQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public ExplorerQuery(IBlockChainContext chainContext)
"nodeState",
resolve: context => chainContext
);
Field<HelperQuery>("helperQuery", resolve: context => new { });

Name = "ExplorerQuery";
}
Expand Down
48 changes: 48 additions & 0 deletions Libplanet.Explorer/Queries/HelperQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Security.Cryptography;
using Bencodex;
using GraphQL;
using GraphQL.Types;
using Libplanet.Common;
using Libplanet.Explorer.GraphTypes;
using Libplanet.Types.Assets;

namespace Libplanet.Explorer.Queries
{
public class HelperQuery : ObjectGraphType
{
private static Codec _codec = new Codec();

public HelperQuery()
{
Name = "HelperQuery";
Description = "A number of queries for convenience.";

Field<NonNullGraphType<HashDigestType<SHA1>>>(
name: "currencyHash",
description: "Converts currency info to currency hash.",
arguments: new QueryArguments(
new QueryArgument<NonNullGraphType<CurrencyInputType>>
{
Name = "currency",
Description = "The currency to convert.",
}
),
resolve: context => context.GetArgument<Currency>("currency").Hash
);

Field<NonNullGraphType<BencodexValueType>>(
name: "bencodexValue",
description: "Decodes hex encoded bencodex value",
arguments: new QueryArguments(
new QueryArgument<NonNullGraphType<StringGraphType>>
{
Name = "hex",
Description = "The byte array in hex representation to decode.",
}
),
resolve: context =>
_codec.Decode(ByteUtil.ParseHex(context.GetArgument<string>("hex")))
);
}
}
}
Loading