Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduced Fusion Export Command #6541

Merged
merged 1 commit into from
Sep 16, 2023
Merged
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
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());
}
}
Loading