Skip to content

Commit

Permalink
Introduce graphql schema command
Browse files Browse the repository at this point in the history
  • Loading branch information
moreal committed Sep 10, 2024
1 parent fe0bdf1 commit f116d0d
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
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

0 comments on commit f116d0d

Please sign in to comment.