Skip to content

Commit 69d7955

Browse files
Add export schema Fusion CLI command (#7008)
1 parent 00b4fdf commit 69d7955

File tree

3 files changed

+77
-2
lines changed

3 files changed

+77
-2
lines changed

src/HotChocolate/Fusion/src/CommandLine/Commands/ExportCommand.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ public ExportCommand() : base("export")
1515
Description = "Export commands.";
1616

1717
AddCommand(new ExportGraphCommand());
18+
AddCommand(new ExportSchemaCommand());
1819
}
1920
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ private static async Task<int> ExecuteAsync(
4646
packageFile = new FileInfo(Combine(packageFile.FullName, "gateway" + FusionPackage));
4747
}
4848
else if (!packageFile.Extension.EqualsOrdinal(FusionPackage) &&
49-
!packageFile.Extension.EqualsOrdinal(ZipPackage))
49+
!packageFile.Extension.EqualsOrdinal(ZipPackage))
5050
{
5151
packageFile = new FileInfo(packageFile.FullName + FusionPackage);
5252
}
@@ -65,7 +65,8 @@ private static async Task<int> ExecuteAsync(
6565
var graph = await package.GetFusionGraphAsync(cancellationToken);
6666
var options = new SyntaxSerializerOptions { Indented = true, MaxDirectivesPerLine = 0, };
6767

68-
await File.WriteAllTextAsync(graphFile.FullName, graph.ToString(options), Encoding.UTF8, cancellationToken);
68+
await File.WriteAllTextAsync(graphFile.FullName, graph.ToString(options),
69+
new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true), cancellationToken);
6970

7071
return 0;
7172
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System.CommandLine;
2+
using System.Text;
3+
using HotChocolate.Fusion.CommandLine.Helpers;
4+
using HotChocolate.Language;
5+
using HotChocolate.Language.Utilities;
6+
using HotChocolate.Utilities;
7+
8+
namespace HotChocolate.Fusion.CommandLine.Commands;
9+
10+
internal sealed class ExportSchemaCommand : Command
11+
{
12+
public ExportSchemaCommand() : base("schema")
13+
{
14+
var fusionPackageFile = new Option<FileInfo?>("--package-file");
15+
fusionPackageFile.AddAlias("--package");
16+
fusionPackageFile.AddAlias("-p");
17+
18+
var schemaFile = new Option<FileInfo?>("--file");
19+
schemaFile.AddAlias("-f");
20+
21+
AddOption(fusionPackageFile);
22+
AddOption(schemaFile);
23+
24+
this.SetHandler(
25+
ExecuteAsync,
26+
Bind.FromServiceProvider<IConsole>(),
27+
fusionPackageFile,
28+
schemaFile,
29+
Bind.FromServiceProvider<CancellationToken>());
30+
}
31+
32+
private static async Task<int> ExecuteAsync(
33+
IConsole console,
34+
FileInfo? packageFile,
35+
FileInfo? schemaFile,
36+
CancellationToken cancellationToken)
37+
{
38+
packageFile ??=
39+
new FileInfo(System.IO.Path.Combine(Environment.CurrentDirectory, "gateway" + Extensions.FusionPackage));
40+
41+
if (!packageFile.Exists)
42+
{
43+
if (Directory.Exists(packageFile.FullName))
44+
{
45+
packageFile =
46+
new FileInfo(System.IO.Path.Combine(packageFile.FullName, "gateway" + Extensions.FusionPackage));
47+
}
48+
else if (!packageFile.Extension.EqualsOrdinal(Extensions.FusionPackage) &&
49+
!packageFile.Extension.EqualsOrdinal(Extensions.ZipPackage))
50+
{
51+
packageFile = new FileInfo(packageFile.FullName + Extensions.FusionPackage);
52+
}
53+
54+
if (!packageFile.Exists)
55+
{
56+
console.WriteLine($"The package file `{packageFile.FullName}` does not exist.");
57+
return 1;
58+
}
59+
}
60+
61+
schemaFile ??= new FileInfo(System.IO.Path.Combine(packageFile.DirectoryName!, "schema.graphql"));
62+
63+
await using var package = FusionGraphPackage.Open(packageFile.FullName);
64+
65+
var schema = await package.GetSchemaAsync(cancellationToken);
66+
var options = new SyntaxSerializerOptions { Indented = true, MaxDirectivesPerLine = 0, };
67+
68+
await File.WriteAllTextAsync(schemaFile.FullName, schema.ToString(options),
69+
new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true), cancellationToken);
70+
71+
return 0;
72+
}
73+
}

0 commit comments

Comments
 (0)