Skip to content

Commit

Permalink
Merge pull request #2 from planetarium/use-graphql-code-generator
Browse files Browse the repository at this point in the history
PDX-321: Use graphql code generator
  • Loading branch information
moreal authored Feb 1, 2024
2 parents 40f1802 + c4b8627 commit ae20631
Show file tree
Hide file tree
Showing 13 changed files with 1,493 additions and 34 deletions.
12 changes: 12 additions & 0 deletions .config/dotnet-tools.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": 1,
"isRoot": true,
"tools": {
"strawberryshake.tools": {
"version": "13.8.1",
"commands": [
"dotnet-graphql"
]
}
}
}
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -280,4 +280,7 @@ obj/
/node_modules
/wwwroot/node_modules

# End of https://www.toptal.com/developers/gitignore/api/dotnetcore,aspnetcore
# End of https://www.toptal.com/developers/gitignore/api/dotnetcore,aspnetcore

# StrawberryShake
Generated
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ WORKDIR /app

# Copy everything else and build
COPY . ./
RUN dotnet tool restore
RUN dotnet graphql generate NineChroniclesUtilBackend
RUN <<EOF
#!/bin/bash
echo "TARGETPLATFROM=$TARGETPLATFORM"
Expand Down
21 changes: 21 additions & 0 deletions NineChroniclesUtilBackend/.graphqlrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"schema": "schema.graphql",
"documents": "**/*.graphql",
"extensions": {
"strawberryShake": {
"name": "HeadlessGQLClient",
"namespace": "HeadlessGQL",
"url": "https://9c-main-full-state.nine-chronicles.com/graphql",
"records": {
"inputs": false,
"entities": false
},
"transportProfiles": [
{
"default": "Http",
"subscription": "WebSocket"
}
]
}
}
}
2 changes: 2 additions & 0 deletions NineChroniclesUtilBackend/NineChroniclesUtilBackend.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
<PackageReference Include="Lib9c" Version="1.1.0-dev.e1742b5c9a96d9b86a329e796c9c6e13e874ba0c" />
<PackageReference Include="Libplanet" Version="3.9.3" />
<PackageReference Include="StrawberryShake" Version="13.8.1" />
<PackageReference Include="StrawberryShake.Transport.Http" Version="13.8.1" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace NineChroniclesUtilBackend.Options;

public class HeadlessStateServiceOption
{
public Uri HeadlessEndpoint { get; set; }
}
7 changes: 6 additions & 1 deletion NineChroniclesUtilBackend/Program.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System.Text.Json.Serialization;
using Microsoft.Extensions.Options;
using NineChroniclesUtilBackend.Services;
using NineChroniclesUtilBackend.Options;

var builder = WebApplication.CreateBuilder(args);

builder.Configuration.AddEnvironmentVariables();

builder.Services.Configure<HeadlessStateServiceOptions>(builder.Configuration.GetRequiredSection("StateService"));
builder.Services.Configure<HeadlessStateServiceOption>(builder.Configuration.GetRequiredSection("StateService"));
builder.Services.ConfigureHttpJsonOptions(options =>
options.SerializerOptions.Converters.Add(new JsonStringEnumConverter()));

Expand All @@ -16,6 +18,9 @@
});
builder.Services.AddSingleton<IStateService, HeadlessStateService>();
builder.Services.AddControllers();
builder.Services.AddHeadlessGQLClient()
.ConfigureHttpClient((provider, client) =>
client.BaseAddress = provider.GetRequiredService<IOptions<HeadlessStateServiceOption>>().Value.HeadlessEndpoint);

var app = builder.Build();

Expand Down
11 changes: 11 additions & 0 deletions NineChroniclesUtilBackend/Queries.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
query GetTip {
nodeStatus {
tip {
index
}
}
}

query GetState($address: Address!) {
state(address: $address)
}
44 changes: 18 additions & 26 deletions NineChroniclesUtilBackend/Services/HeadlessStateService.cs
Original file line number Diff line number Diff line change
@@ -1,48 +1,40 @@
using System.Text;
using System.Text.Json;
using Bencodex;
using Bencodex.Types;
using HeadlessGQL;
using Libplanet.Crypto;
using Microsoft.Extensions.Options;
using Libplanet.Types.Blocks;
using StrawberryShake;

namespace NineChroniclesUtilBackend.Services;

public class HeadlessStateService(IOptions<HeadlessStateServiceOptions> options) : IStateService
public class HeadlessStateService(IHeadlessGQLClient client) : IStateService
{
private readonly Uri _headlessEndpoint = options.Value.HeadlessEndpoint;
private readonly HttpClient _httpClient = new();

private static readonly Codec Codec = new();

private (long, BlockHash) TipInfo { get; set; }

public long TipIndex => TipInfo.Item1;

public Task<IValue?[]> GetStates(Address[] addresses)
{
return Task.WhenAll(addresses.Select(GetState));
}

public async Task<IValue?> GetState(Address address)
{
using StringContent jsonContent = new
(JsonSerializer.Serialize(new
{
query = @"query GetState($address: Address!) { state(address: $address) }",
variables = new {
address = address.ToString(),
},
operationName = "GetState",
}),
Encoding.UTF8,
"application/json");
using var response = await _httpClient.PostAsync(_headlessEndpoint, jsonContent);
var resp = await response.Content.ReadFromJsonAsync<GetStateResponse>();

if (resp.Data.State is null)
var result = await client.GetState.ExecuteAsync(address.ToString());
result.EnsureNoErrors();

if (result.Data?.State is null)
{
return null;
}

return Codec.Decode(Convert.FromHexString(resp.Data.State));
return Codec.Decode(Convert.FromHexString(result.Data.State));
}
}

record GetStateResponse(GetStateResponseData Data, object Errors);
record GetStateResponseData(string? State);
private static void UpdateTipIndex()
{

}
}

This file was deleted.

13 changes: 13 additions & 0 deletions NineChroniclesUtilBackend/schema.extensions.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
scalar _KeyFieldSet

directive @key(fields: _KeyFieldSet!) on SCHEMA | OBJECT

directive @serializationType(name: String!) on SCALAR

directive @runtimeType(name: String!) on SCALAR

directive @enumValue(value: String!) on ENUM_VALUE

directive @rename(name: String!) on INPUT_FIELD_DEFINITION | INPUT_OBJECT | ENUM | ENUM_VALUE

extend schema @key(fields: "id")
Loading

0 comments on commit ae20631

Please sign in to comment.