Skip to content

Commit 87c992c

Browse files
authored
Added schema-settings refinements (#8511)
1 parent b6f951f commit 87c992c

9 files changed

+174
-21
lines changed

src/HotChocolate/AspNetCore/src/AspNetCore.CommandLine/Command/ExportCommand.cs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ internal sealed class ExportCommand : Command
1919
/// </summary>
2020
public ExportCommand() : base("export")
2121
{
22-
Description =
23-
"Export the graphql schema. If no output (--output) is specified the schema will be "
24-
+ "printed to the console.";
22+
Description = "Export the graphql schema to a schema file";
2523

2624
AddOption(Opt<OutputOption>.Instance);
2725
AddOption(Opt<SchemaNameOption>.Instance);
@@ -60,20 +58,13 @@ private static async Task ExecuteAsync(
6058
}
6159

6260
var executor = await provider.GetExecutorAsync(schemaName, cancellationToken);
61+
output ??= new FileInfo(System.IO.Path.Combine(Environment.CurrentDirectory, "schema.graphqls"));
62+
var result = await SchemaFileExporter.Export(output.FullName, executor, cancellationToken);
6363

64-
if (output is not null)
65-
{
66-
var result = await SchemaFileExporter.Export(output.FullName, executor, cancellationToken);
67-
// ReSharper disable LocalizableElement
68-
console.WriteLine("Exported Files:");
69-
console.WriteLine($"- {result.SchemaFileName}");
70-
console.WriteLine($"- {result.SettingsFileName}");
71-
// ReSharper restore LocalizableElement
72-
}
73-
else
74-
{
75-
var sdl = executor.Schema.ToString();
76-
console.WriteLine(sdl);
77-
}
64+
// ReSharper disable LocalizableElement
65+
console.WriteLine("Exported Files:");
66+
console.WriteLine($"- {result.SchemaFileName}");
67+
console.WriteLine($"- {result.SettingsFileName}");
68+
// ReSharper restore LocalizableElement
7869
}
7970
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System.CommandLine;
2+
using System.Text;
3+
using System.Text.Json;
4+
using System.Text.Json.Nodes;
5+
using HotChocolate.Execution;
6+
using HotChocolate.Execution.Internal;
7+
using Microsoft.Extensions.DependencyInjection;
8+
using Microsoft.Extensions.Hosting;
9+
10+
namespace HotChocolate.AspNetCore.CommandLine;
11+
12+
/// <summary>
13+
/// The print command can be used to print the schema to the console output
14+
/// </summary>
15+
internal sealed class PrintCommand : Command
16+
{
17+
/// <summary>
18+
/// Initializes a new instance of the <see cref="ExportCommand"/> class.
19+
/// </summary>
20+
public PrintCommand() : base("print")
21+
{
22+
Description = "Prints the graphql schema to the console output";
23+
24+
AddOption(Opt<SchemaNameOption>.Instance);
25+
26+
this.SetHandler(
27+
ExecuteAsync,
28+
Bind.FromServiceProvider<IConsole>(),
29+
Bind.FromServiceProvider<IHost>(),
30+
Opt<SchemaNameOption>.Instance,
31+
Bind.FromServiceProvider<CancellationToken>());
32+
}
33+
34+
private static async Task ExecuteAsync(
35+
IConsole console,
36+
IHost host,
37+
string? schemaName,
38+
CancellationToken cancellationToken)
39+
{
40+
var provider = host.Services.GetRequiredService<IRequestExecutorProvider>();
41+
42+
if (schemaName is null)
43+
{
44+
var schemaNames = provider.SchemaNames;
45+
46+
if (schemaNames.IsEmpty)
47+
{
48+
console.WriteLine("No schemas registered.");
49+
return;
50+
}
51+
52+
schemaName = schemaNames.Contains(ISchemaDefinition.DefaultName)
53+
? ISchemaDefinition.DefaultName
54+
: schemaNames[0];
55+
}
56+
57+
var executor = await provider.GetExecutorAsync(schemaName, cancellationToken);
58+
console.WriteLine(executor.Schema.ToString());
59+
}
60+
}

src/HotChocolate/AspNetCore/src/AspNetCore.CommandLine/Command/SchemaCommand.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public SchemaCommand() : base("schema")
1515
Description = "Schema management commands.";
1616

1717
AddCommand(new ExportCommand());
18+
AddCommand(new PrintCommand());
1819
AddCommand(new ListCommand());
1920
}
2021
}

src/HotChocolate/AspNetCore/src/AspNetCore/Extensions/HotChocolateAspNetCoreServiceCollectionExtensions.Warmup.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@ public static IRequestExecutorBuilder InitializeOnStartup(
3737
return builder;
3838
}
3939

40+
/// <summary>
41+
/// Adds the current GraphQL configuration to the warmup background service.
42+
/// </summary>
43+
/// <param name="builder">
44+
/// The <see cref="IRequestExecutorBuilder"/>.
45+
/// </param>
46+
/// <param name="options">
47+
/// The <see cref="RequestExecutorInitializationOptions"/>.
48+
/// </param>
49+
/// <returns>
50+
/// Returns the <see cref="IRequestExecutorBuilder"/> so that configuration can be chained.
51+
/// </returns>
52+
/// <exception cref="ArgumentNullException">
53+
/// The <see cref="IRequestExecutorBuilder"/> is <c>null</c>.
54+
/// </exception>
4055
public static IRequestExecutorBuilder InitializeOnStartup(
4156
this IRequestExecutorBuilder builder,
4257
RequestExecutorInitializationOptions options)
@@ -72,4 +87,36 @@ public static IRequestExecutorBuilder InitializeOnStartup(
7287

7388
return InitializeOnStartup(builder, warmup, options.KeepWarm);
7489
}
90+
91+
/// <summary>
92+
/// Exports the GraphQL schema to a file on startup or when the schema changes.
93+
/// </summary>
94+
/// <param name="builder">
95+
/// The <see cref="IRequestExecutorBuilder"/>.
96+
/// </param>
97+
/// <param name="schemaFileName">
98+
/// The file name of the schema file.
99+
/// </param>
100+
/// <returns>
101+
/// Returns the <see cref="IRequestExecutorBuilder"/> so that configuration can be chained.
102+
/// </returns>
103+
/// <exception cref="ArgumentNullException">
104+
/// The <see cref="IRequestExecutorBuilder"/> is <c>null</c>.
105+
/// </exception>
106+
public static IRequestExecutorBuilder ExportSchemaOnStartup(
107+
this IRequestExecutorBuilder builder,
108+
string? schemaFileName = null)
109+
{
110+
ArgumentNullException.ThrowIfNull(builder);
111+
112+
return InitializeOnStartup(builder, new RequestExecutorInitializationOptions
113+
{
114+
KeepWarm = true,
115+
WriteSchemaFile = new SchemaFileInitializationOptions
116+
{
117+
Enable = true,
118+
FileName = schemaFileName
119+
}
120+
});
121+
}
75122
}

src/HotChocolate/AspNetCore/test/AspNetCore.CommandLine.Tests/SchemaExportCommandTests.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,21 @@ public async Task App_Should_OutputCorrectHelpTest_When_HelpIsRequested()
2626
console.Out.ToString().MatchSnapshot();
2727
}
2828

29+
[Fact]
30+
public async Task App_Should_OutputCorrectHelpTest_When_Print_HelpIsRequested()
31+
{
32+
// arrange
33+
var host = new Mock<IHost>().Object;
34+
var console = new TestConsole();
35+
var app = new App(host).Build();
36+
37+
// act
38+
await app.InvokeAsync("schema print -h", console);
39+
40+
// assert
41+
console.Out.ToString().MatchSnapshot();
42+
}
43+
2944
[Fact]
3045
public async Task App_Should_PrintSchema_When_OutputNotSpecified()
3146
{
@@ -44,7 +59,7 @@ public async Task App_Should_PrintSchema_When_OutputNotSpecified()
4459
var app = new App(host).Build();
4560

4661
// act
47-
await app.InvokeAsync("schema export", console);
62+
await app.InvokeAsync("schema print", console);
4863

4964
// assert
5065
console.Out.ToString().MatchSnapshot();
@@ -96,7 +111,7 @@ public async Task App_Should_WriteNamedSchemaToOutput_When_SchemaNameIsSpecified
96111
var app = new App(host).Build();
97112

98113
// act
99-
await app.InvokeAsync("schema export --schema-name Foo", console);
114+
await app.InvokeAsync("schema print --schema-name Foo", console);
100115

101116
// assert
102117
console.Out.ToString().MatchSnapshot();

src/HotChocolate/AspNetCore/test/AspNetCore.CommandLine.Tests/__snapshots__/SchemaCommandTests.App_Should_OutputCorrectHelpTest_When_HelpIsRequested.snap

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Options:
88
-?, -h, --help Show help and usage information
99

1010
Commands:
11-
export Export the graphql schema. If no output (--output) is specified the schema will be printed to the console.
11+
export Export the graphql schema to a schema file
12+
print Prints the graphql schema to the console output
1213
list List all registered GraphQL schemas.
1314

src/HotChocolate/AspNetCore/test/AspNetCore.CommandLine.Tests/__snapshots__/SchemaExportCommandTests.App_Should_OutputCorrectHelpTest_When_HelpIsRequested.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Description:
2-
Export the graphql schema. If no output (--output) is specified the schema will be printed to the console.
2+
Export the graphql schema to a schema file
33

44
Usage:
55
graphql schema export [options]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Description:
2+
Prints the graphql schema to the console output
3+
4+
Usage:
5+
graphql schema print [options]
6+
7+
Options:
8+
--schema-name <schema-name> The name of the schema that should be exported. If no schema name is specified the default schema will be exported.
9+
-?, -h, --help Show help and usage information
10+
11+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# App_Should_WriteSchemaToFile_When_OutputOptionIsSpecified
2+
3+
## Schema
4+
5+
```graphql
6+
schema {
7+
query: Query
8+
}
9+
10+
type Query {
11+
foo: String
12+
}
13+
```
14+
15+
## Settings
16+
17+
```json
18+
{
19+
"name": "_Default",
20+
"transports": {
21+
"http": {
22+
"url": "http://localhost:5000/graphql"
23+
}
24+
}
25+
}
26+
```
27+

0 commit comments

Comments
 (0)