Skip to content

Commit 2043e2d

Browse files
authored
Fix schema initialization exceptions (graphql-dotnet#3445)
1 parent 997dfaa commit 2043e2d

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

src/GraphQL.Tests/Bugs/BugReport.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using GraphQL.Types;
2+
3+
namespace GraphQL.Tests.Bugs;
4+
5+
public class Bug3444
6+
{
7+
[Fact]
8+
public void Rethrow_Schema_Initialization_Failure()
9+
{
10+
var queryType = new ObjectGraphType { Name = "Query" };
11+
queryType.Field<TestOneType>("test1");
12+
queryType.Field<TestTwoType>("test2");
13+
var schema = new Schema() { Query = queryType };
14+
var msg1 = Should.Throw<InvalidOperationException>(() => schema.Initialize()).Message;
15+
var msg2 = Should.Throw<InvalidOperationException>(() => schema.Initialize()).Message;
16+
msg1.ShouldBe(msg2);
17+
}
18+
19+
private class TestOneType : EnumerationGraphType<TestEnum>
20+
{
21+
}
22+
23+
private class TestTwoType : EnumerationGraphType<TestEnum>
24+
{
25+
}
26+
27+
private enum TestEnum
28+
{
29+
One,
30+
Two,
31+
Three
32+
}
33+
}

src/GraphQL/Types/Schema.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Diagnostics;
2+
using System.Runtime.ExceptionServices;
23
using GraphQL.Conversion;
34
using GraphQL.DI;
45
using GraphQL.Instrumentation;
@@ -74,6 +75,7 @@ public SchemaDebugView(Schema schema)
7475
private bool _disposed;
7576
private IServiceProvider _services;
7677
private SchemaTypes? _allTypes;
78+
private ExceptionDispatchInfo? _initializationException;
7779
private readonly object _allTypesInitializationLock = new();
7880

7981
private List<Type>? _additionalTypes;
@@ -201,9 +203,19 @@ public void Initialize()
201203
if (Initialized)
202204
return;
203205

204-
CreateAndInitializeSchemaTypes();
206+
_initializationException?.Throw();
205207

206-
Initialized = true;
208+
try
209+
{
210+
CreateAndInitializeSchemaTypes();
211+
212+
Initialized = true;
213+
}
214+
catch (Exception ex)
215+
{
216+
_initializationException = ExceptionDispatchInfo.Capture(ex);
217+
throw;
218+
}
207219
}
208220
}
209221

0 commit comments

Comments
 (0)