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

Add export schema Fusion CLI command #7008

Merged
merged 1 commit into from
Mar 22, 2024
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
Expand Up @@ -15,5 +15,6 @@ public ExportCommand() : base("export")
Description = "Export commands.";

AddCommand(new ExportGraphCommand());
AddCommand(new ExportSchemaCommand());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
packageFile = new FileInfo(Combine(packageFile.FullName, "gateway" + FusionPackage));
}
else if (!packageFile.Extension.EqualsOrdinal(FusionPackage) &&
!packageFile.Extension.EqualsOrdinal(ZipPackage))
!packageFile.Extension.EqualsOrdinal(ZipPackage))

Check warning on line 49 in src/HotChocolate/Fusion/src/CommandLine/Commands/ExportGraphCommand.cs

View check run for this annotation

Codecov / codecov/patch

src/HotChocolate/Fusion/src/CommandLine/Commands/ExportGraphCommand.cs#L49

Added line #L49 was not covered by tests
{
packageFile = new FileInfo(packageFile.FullName + FusionPackage);
}
Expand All @@ -65,7 +65,8 @@
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),

Check warning on line 68 in src/HotChocolate/Fusion/src/CommandLine/Commands/ExportGraphCommand.cs

View check run for this annotation

Codecov / codecov/patch

src/HotChocolate/Fusion/src/CommandLine/Commands/ExportGraphCommand.cs#L68

Added line #L68 was not covered by tests
new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true), cancellationToken);

return 0;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<FileInfo?>("--package-file");
fusionPackageFile.AddAlias("--package");
fusionPackageFile.AddAlias("-p");

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

AddOption(fusionPackageFile);
AddOption(schemaFile);

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

private static async Task<int> 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;
}
}
Loading