Skip to content

Commit

Permalink
Introduced Fusion Export Command (#6541)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib authored Sep 16, 2023
1 parent dd6c3cd commit af204e7
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.CommandLine;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using HotChocolate.Execution;
Expand Down Expand Up @@ -51,7 +52,7 @@ private static async Task ExecuteAsync(

if (output is { })
{
await File.WriteAllTextAsync(output.FullName, sdl, cancellationToken);
await File.WriteAllTextAsync(output.FullName, sdl, Encoding.UTF8, cancellationToken);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.CommandLine;
using System.Diagnostics.CodeAnalysis;
using System.IO.Packaging;
using System.Text.Json;
using System.Text.Json.Serialization;
using HotChocolate.Fusion.CommandLine.Helpers;
Expand Down
19 changes: 19 additions & 0 deletions src/HotChocolate/Fusion/src/CommandLine/Commands/ExportCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.CommandLine;

namespace HotChocolate.Fusion.CommandLine.Commands;

/// <summary>
/// The export command
/// </summary>
internal sealed class ExportCommand : Command
{
/// <summary>
/// Initializes a new instance of <see cref="ExportCommand"/>.
/// </summary>
public ExportCommand() : base("export")
{
Description = "Export commands.";

AddCommand(new ExportGraphCommand());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System.CommandLine;
using System.Text;
using HotChocolate.Fusion.CommandLine.Helpers;
using HotChocolate.Utilities;
using static System.IO.Path;
using static HotChocolate.Fusion.CommandLine.Extensions;

namespace HotChocolate.Fusion.CommandLine.Commands;

internal sealed class ExportGraphCommand : Command
{
public ExportGraphCommand() : base("graph")
{
var fusionPackageFile = new Option<FileInfo?>("--package-file");
fusionPackageFile.AddAlias("--package");
fusionPackageFile.AddAlias("-p");

var graphFile = new Option<FileInfo?>("--file");
graphFile.AddAlias("-f");

AddOption(fusionPackageFile);
AddOption(graphFile);

this.SetHandler(
ExecuteAsync,
Bind.FromServiceProvider<IConsole>(),
fusionPackageFile,
graphFile,
Bind.FromServiceProvider<CancellationToken>());
}

private static async Task ExecuteAsync(
IConsole console,
FileInfo? packageFile,
FileInfo? graphFile,
CancellationToken cancellationToken)
{
packageFile ??= new FileInfo(Combine(Environment.CurrentDirectory, "gateway" + FusionPackage));

if (!packageFile.Exists)
{
if (Directory.Exists(packageFile.FullName))
{
packageFile = new FileInfo(Combine(packageFile.FullName, "gateway" + FusionPackage));
}
else if (!packageFile.Extension.EqualsOrdinal(FusionPackage) &&
!packageFile.Extension.EqualsOrdinal(ZipPackage))
{
packageFile = new FileInfo(packageFile.FullName + FusionPackage);
}

if (!packageFile.Exists)
{
console.WriteLine($"The package file `{packageFile.FullName}` does not exist.");
return;
}
}

graphFile ??= new FileInfo(Combine(packageFile.DirectoryName!, "fusion.graphql"));

await using var package = FusionGraphPackage.Open(packageFile.FullName);
var graph = await package.GetFusionGraphAsync(cancellationToken);
await File.WriteAllTextAsync(graphFile.FullName, graph.ToString(true), Encoding.UTF8, cancellationToken);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.CommandLine;
using System.Diagnostics.CodeAnalysis;
using static System.IO.Path;

namespace HotChocolate.Fusion.CommandLine.Commands;
Expand All @@ -11,11 +12,13 @@ internal sealed class RootCommand : Command
/// <summary>
/// Initializes a new instance of <see cref="RootCommand"/>.
/// </summary>
[RequiresUnreferencedCode("Calls HotChocolate.Fusion.CommandLine.Commands.ComposeCommand.ComposeCommand()")]
public RootCommand() : base("fusion")
{
Description = "A command line tool for a Hot Chocolate Fusion.";

AddCommand(new ComposeCommand());
AddCommand(new SubgraphCommand());
AddCommand(new ExportCommand());
}
}

0 comments on commit af204e7

Please sign in to comment.