Skip to content

Commit cdba759

Browse files
Return exit code from RunWithGraphQLCommands (#8772)
1 parent 7b8a56a commit cdba759

File tree

3 files changed

+48
-13
lines changed

3 files changed

+48
-13
lines changed

src/HotChocolate/AspNetCore/src/AspNetCore.CommandLine/WebApplicationExtensions.cs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static class HostBuilderExtensions
1919
/// </example>
2020
/// <param name="builder">The IHostBuilder instance</param>
2121
/// <param name="args">Command line arguments to be passed to the host </param>
22-
public static async Task RunWithGraphQLCommandsAsync(
22+
public static async Task<int> RunWithGraphQLCommandsAsync(
2323
this IHostBuilder builder,
2424
string[] args)
2525
=> await builder.Build().RunWithGraphQLCommandsAsync(args);
@@ -35,7 +35,7 @@ public static async Task RunWithGraphQLCommandsAsync(
3535
/// </example>
3636
/// <param name="builder">The IHostBuilder instance</param>
3737
/// <param name="args">Command line arguments to be passed to the host </param>
38-
public static void RunWithGraphQLCommands(
38+
public static int RunWithGraphQLCommands(
3939
this IHostBuilder builder,
4040
string[] args)
4141
=> builder.Build().RunWithGraphQLCommands(args);
@@ -57,13 +57,11 @@ public static async Task<int> RunWithGraphQLCommandsAsync(
5757
{
5858
if (args.IsGraphQLCommand())
5959
{
60-
await new App(host).Build().InvokeAsync(args);
61-
}
62-
else
63-
{
64-
await host.RunAsync();
60+
return await new App(host).Build().InvokeAsync(args);
6561
}
6662

63+
await host.RunAsync();
64+
6765
return 0;
6866
}
6967

@@ -78,18 +76,18 @@ public static async Task<int> RunWithGraphQLCommandsAsync(
7876
/// </example>
7977
/// <param name="host">The IHost instance</param>
8078
/// <param name="args">Command line arguments to be passed to the host </param>
81-
public static void RunWithGraphQLCommands(
79+
public static int RunWithGraphQLCommands(
8280
this IHost host,
8381
string[] args)
8482
{
8583
if (args.IsGraphQLCommand())
8684
{
87-
new App(host).Build().Invoke(args);
88-
}
89-
else
90-
{
91-
host.Run();
85+
return new App(host).Build().Invoke(args);
9286
}
87+
88+
host.Run();
89+
90+
return 0;
9391
}
9492

9593
/// <summary>

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,28 @@ public async Task App_Should_WriteNamedSchemaToOutput_When_SchemaNameIsSpecified
117117
console.Out.ToString().MatchSnapshot();
118118
}
119119

120+
[Fact]
121+
public async Task App_Should_Return_ExitCode_1_If_Schema_Is_Invalid()
122+
{
123+
// arrange
124+
var services = new ServiceCollection();
125+
// We're intentionally no specifying any fields here to create an invalid schema.
126+
services.AddGraphQL().AddQueryType();
127+
128+
var hostMock = new Mock<IHost>();
129+
hostMock
130+
.Setup(x => x.Services)
131+
.Returns(services.BuildServiceProvider());
132+
133+
var host = hostMock.Object;
134+
135+
// act
136+
var exitCode = await host.RunWithGraphQLCommandsAsync(["schema", "print"]);
137+
138+
// assert
139+
Assert.Equal(1, exitCode);
140+
}
141+
120142
public string CreateSchemaFileName()
121143
{
122144
var tempFile = System.IO.Path.GetTempFileName();

website/src/docs/hotchocolate/v16/migrating/migrate-from-15-to-16.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,18 @@ In addition, the default output for such errors has been standardized: earlier,
144144
# Deprecations
145145

146146
Things that will continue to function this release, but we encourage you to move away from.
147+
148+
# Noteworthy changes
149+
150+
## RunWithGraphQLCommandsAsync returns exit code
151+
152+
`RunWithGraphQLCommandsAsync` and `RunWithGraphQLCommands` now return exit codes (`Task<int>` and `int` respectively, instead of `Task` and `void`).
153+
154+
We recommend updating your `Program.cs` to return this exit code. This ensures that command failures signal an error to shell scripts, CI/CD pipelines, and other tools:
155+
156+
```diff
157+
var app = builder.Build();
158+
159+
- await app.RunWithGraphQLCommandsAsync(args);
160+
+ return await app.RunWithGraphQLCommandsAsync(args);
161+
```

0 commit comments

Comments
 (0)