diff --git a/NineChronicles.Headless.Executable/Commands/GraphQLCommand.cs b/NineChronicles.Headless.Executable/Commands/GraphQLCommand.cs new file mode 100644 index 000000000..dee14b357 --- /dev/null +++ b/NineChronicles.Headless.Executable/Commands/GraphQLCommand.cs @@ -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(); + serviceCollection.AddSingleton(new ConfigurationBuilder().Build()); + serviceCollection.AddSingleton(); + serviceCollection.AddSingleton(); + serviceCollection.AddSingleton(); + serviceCollection.AddSingleton(); + serviceCollection.AddSingleton( + new BlockChainStates( + new MemoryStore(), + new TrieStateStore(new MemoryKeyValueStore()))); + serviceCollection.AddSingleton("STRING"); // Invalid usage but not care in this case. + serviceCollection.AddSingleton(); + serviceCollection.AddSingleton(services => new ActionEvaluationPublisher( + services.GetRequiredService(), + services.GetRequiredService(), + services.GetRequiredService(), + services.GetRequiredService(), + services.GetRequiredService(), + "host", + 0, + services.GetRequiredService(), + services.GetRequiredService() + )); + serviceCollection.AddSingleton(new NineChroniclesNodeService( + new PrivateKey(), + new LibplanetNodeServiceProperties + { + SwarmPrivateKey = new PrivateKey(), + Host = "localhost", + IceServers = Array.Empty(), + StorePath = Path.GetRandomFileName(), + GenesisBlock = BlockChain.ProposeGenesisBlock(), + }, + new BlockPolicy(), + Planet.Heimdall, + new NCActionLoader())); + serviceCollection.AddSingleton(services => new StandaloneContext + { + NineChroniclesNodeService = services.GetRequiredService(), + }); + + serviceCollection.AddGraphQL() + .AddLibplanetExplorer() + .AddGraphTypes(typeof(StandaloneSchema)); + + IServiceProvider serviceProvider = serviceCollection.BuildServiceProvider(); + var schema = new StandaloneSchema(serviceProvider); + var printer = new SchemaPrinter(schema); + + Console.WriteLine(printer.Print()); + } +} diff --git a/NineChronicles.Headless.Executable/Program.cs b/NineChronicles.Headless.Executable/Program.cs index 125cd02aa..7c19f7010 100644 --- a/NineChronicles.Headless.Executable/Program.cs +++ b/NineChronicles.Headless.Executable/Program.cs @@ -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) diff --git a/NineChronicles.Headless/GraphTypes/StandaloneSubscription.cs b/NineChronicles.Headless/GraphTypes/StandaloneSubscription.cs index af0600912..4ab19cf5b 100644 --- a/NineChronicles.Headless/GraphTypes/StandaloneSubscription.cs +++ b/NineChronicles.Headless/GraphTypes/StandaloneSubscription.cs @@ -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); }