-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from planetarium/use-graphql-code-generator
PDX-321: Use graphql code generator
- Loading branch information
Showing
13 changed files
with
1,493 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
NineChroniclesUtilBackend/Options/HeadlessStateServiceOption.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
44
NineChroniclesUtilBackend/Services/HeadlessStateService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
{ | ||
|
||
} | ||
} |
6 changes: 0 additions & 6 deletions
6
NineChroniclesUtilBackend/Services/HeadlessStateServiceOptions.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
Oops, something went wrong.