Skip to content
Draft
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
81 changes: 81 additions & 0 deletions NineChronicles.Headless.Executable/Commands/GraphQLCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System;
using System.Collections.Generic;
using System.IO;
using GraphQL.Server;
using GraphQL.Utilities;
using Lib9c.Renderers;
using Libplanet.Action.State;
using Libplanet.Blockchain;
using Libplanet.Blockchain.Policies;
using Libplanet.Crypto;
using Libplanet.Headless.Hosting;
using Libplanet.Net;
using Libplanet.Store;
using Libplanet.Store.Trie;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Nekoyume;
using Nekoyume.Action.Loader;
using NineChronicles.Headless.GraphTypes;

namespace NineChronicles.Headless.Executable.Commands;

public class GraphQLCommand
{
[Cocona.Command]
public void Schema()
{
var serviceCollection = new ServiceCollection();

serviceCollection.AddSingleton<StateMemoryCache>();
serviceCollection.AddSingleton<IConfiguration>(new ConfigurationBuilder().Build());
serviceCollection.AddSingleton<BlockRenderer>();
serviceCollection.AddSingleton<ActionRenderer>();
serviceCollection.AddSingleton<ExceptionRenderer>();
serviceCollection.AddSingleton<NodeStatusRenderer>();
serviceCollection.AddSingleton<IBlockChainStates>(
new BlockChainStates(
new MemoryStore(),
new TrieStateStore(new MemoryKeyValueStore())));
serviceCollection.AddSingleton<string>("STRING"); // Invalid usage but not care in this case.
serviceCollection.AddSingleton<RpcContext>();
serviceCollection.AddSingleton<ActionEvaluationPublisher>(services => new ActionEvaluationPublisher(
services.GetRequiredService<BlockRenderer>(),
services.GetRequiredService<ActionRenderer>(),
services.GetRequiredService<ExceptionRenderer>(),
services.GetRequiredService<NodeStatusRenderer>(),
services.GetRequiredService<IBlockChainStates>(),
"host",
0,
services.GetRequiredService<RpcContext>(),
services.GetRequiredService<StateMemoryCache>()
));
serviceCollection.AddSingleton(new NineChroniclesNodeService(
new PrivateKey(),
new LibplanetNodeServiceProperties
{
SwarmPrivateKey = new PrivateKey(),
Host = "localhost",
IceServers = Array.Empty<IceServer>(),
StorePath = Path.GetRandomFileName(),
GenesisBlock = BlockChain.ProposeGenesisBlock(),
},
new BlockPolicy(),
Planet.Heimdall,
new NCActionLoader()));
serviceCollection.AddSingleton<StandaloneContext>(services => new StandaloneContext
{
NineChroniclesNodeService = services.GetRequiredService<NineChroniclesNodeService>(),
});

serviceCollection.AddGraphQL()
.AddLibplanetExplorer()
.AddGraphTypes(typeof(StandaloneSchema));

IServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();
var schema = new StandaloneSchema(serviceProvider);
var printer = new SchemaPrinter(schema);

Console.WriteLine(printer.Print());
}
}
1 change: 1 addition & 0 deletions NineChronicles.Headless.Executable/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ namespace NineChronicles.Headless.Executable
[HasSubCommands(typeof(MarketCommand), "market")]
[HasSubCommands(typeof(GenesisCommand), "genesis")]
[HasSubCommands(typeof(ReplayCommand), "replay")]
[HasSubCommands(typeof(GraphQLCommand), "graphql")]
public class Program : CoconaLiteConsoleAppBase
{
static async Task Main(string[] args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public StandaloneSubscription(StandaloneContext standaloneContext, IConfiguratio
{
StandaloneContext = standaloneContext;
Configuration = configuration;
if (Convert.ToBoolean(configuration.GetSection("Jwt")["EnableJwtAuthentication"]))
if (configuration.GetSection("Jwt")["EnableJwtAuthentication"] is { } enableJwtAuthentication && Convert.ToBoolean(enableJwtAuthentication))
{
this.AuthorizeWith(GraphQLService.JwtPolicyKey);
}
Expand Down