From 49311f7dba6a99f19875540682debc762f7b9d2b Mon Sep 17 00:00:00 2001 From: tobias-tengler <45513122+tobias-tengler@users.noreply.github.com> Date: Fri, 22 Mar 2024 13:34:23 +0100 Subject: [PATCH] Add export schema Fusion CLI command --- .../src/CommandLine/Commands/ExportCommand.cs | 1 + .../Commands/ExportGraphCommand.cs | 5 +- .../Commands/ExportSchemaCommand.cs | 73 +++++++++++++++++++ 3 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 src/HotChocolate/Fusion/src/CommandLine/Commands/ExportSchemaCommand.cs diff --git a/src/HotChocolate/Fusion/src/CommandLine/Commands/ExportCommand.cs b/src/HotChocolate/Fusion/src/CommandLine/Commands/ExportCommand.cs index b14c52f8b34..a6224fb8384 100644 --- a/src/HotChocolate/Fusion/src/CommandLine/Commands/ExportCommand.cs +++ b/src/HotChocolate/Fusion/src/CommandLine/Commands/ExportCommand.cs @@ -15,5 +15,6 @@ public ExportCommand() : base("export") Description = "Export commands."; AddCommand(new ExportGraphCommand()); + AddCommand(new ExportSchemaCommand()); } } diff --git a/src/HotChocolate/Fusion/src/CommandLine/Commands/ExportGraphCommand.cs b/src/HotChocolate/Fusion/src/CommandLine/Commands/ExportGraphCommand.cs index 059cfde2778..b84957ca0ee 100644 --- a/src/HotChocolate/Fusion/src/CommandLine/Commands/ExportGraphCommand.cs +++ b/src/HotChocolate/Fusion/src/CommandLine/Commands/ExportGraphCommand.cs @@ -46,7 +46,7 @@ private static async Task ExecuteAsync( packageFile = new FileInfo(Combine(packageFile.FullName, "gateway" + FusionPackage)); } else if (!packageFile.Extension.EqualsOrdinal(FusionPackage) && - !packageFile.Extension.EqualsOrdinal(ZipPackage)) + !packageFile.Extension.EqualsOrdinal(ZipPackage)) { packageFile = new FileInfo(packageFile.FullName + FusionPackage); } @@ -65,7 +65,8 @@ private static async Task ExecuteAsync( var graph = await package.GetFusionGraphAsync(cancellationToken); var options = new SyntaxSerializerOptions { Indented = true, MaxDirectivesPerLine = 0, }; - await File.WriteAllTextAsync(graphFile.FullName, graph.ToString(options), Encoding.UTF8, cancellationToken); + await File.WriteAllTextAsync(graphFile.FullName, graph.ToString(options), + new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true), cancellationToken); return 0; } diff --git a/src/HotChocolate/Fusion/src/CommandLine/Commands/ExportSchemaCommand.cs b/src/HotChocolate/Fusion/src/CommandLine/Commands/ExportSchemaCommand.cs new file mode 100644 index 00000000000..d958b8480cf --- /dev/null +++ b/src/HotChocolate/Fusion/src/CommandLine/Commands/ExportSchemaCommand.cs @@ -0,0 +1,73 @@ +using System.CommandLine; +using System.Text; +using HotChocolate.Fusion.CommandLine.Helpers; +using HotChocolate.Language; +using HotChocolate.Language.Utilities; +using HotChocolate.Utilities; + +namespace HotChocolate.Fusion.CommandLine.Commands; + +internal sealed class ExportSchemaCommand : Command +{ + public ExportSchemaCommand() : base("schema") + { + var fusionPackageFile = new Option("--package-file"); + fusionPackageFile.AddAlias("--package"); + fusionPackageFile.AddAlias("-p"); + + var schemaFile = new Option("--file"); + schemaFile.AddAlias("-f"); + + AddOption(fusionPackageFile); + AddOption(schemaFile); + + this.SetHandler( + ExecuteAsync, + Bind.FromServiceProvider(), + fusionPackageFile, + schemaFile, + Bind.FromServiceProvider()); + } + + private static async Task ExecuteAsync( + IConsole console, + FileInfo? packageFile, + FileInfo? schemaFile, + CancellationToken cancellationToken) + { + packageFile ??= + new FileInfo(System.IO.Path.Combine(Environment.CurrentDirectory, "gateway" + Extensions.FusionPackage)); + + if (!packageFile.Exists) + { + if (Directory.Exists(packageFile.FullName)) + { + packageFile = + new FileInfo(System.IO.Path.Combine(packageFile.FullName, "gateway" + Extensions.FusionPackage)); + } + else if (!packageFile.Extension.EqualsOrdinal(Extensions.FusionPackage) && + !packageFile.Extension.EqualsOrdinal(Extensions.ZipPackage)) + { + packageFile = new FileInfo(packageFile.FullName + Extensions.FusionPackage); + } + + if (!packageFile.Exists) + { + console.WriteLine($"The package file `{packageFile.FullName}` does not exist."); + return 1; + } + } + + schemaFile ??= new FileInfo(System.IO.Path.Combine(packageFile.DirectoryName!, "schema.graphql")); + + await using var package = FusionGraphPackage.Open(packageFile.FullName); + + var schema = await package.GetSchemaAsync(cancellationToken); + var options = new SyntaxSerializerOptions { Indented = true, MaxDirectivesPerLine = 0, }; + + await File.WriteAllTextAsync(schemaFile.FullName, schema.ToString(options), + new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true), cancellationToken); + + return 0; + } +}