Skip to content

Commit 18ea63b

Browse files
authored
Fixed error handling of unhandeled mutation errors (#7101)
1 parent 4ce9fd6 commit 18ea63b

File tree

3 files changed

+66
-6
lines changed

3 files changed

+66
-6
lines changed

src/HotChocolate/Core/src/Types.Mutations/MutationConventionTypeInterceptor.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ private static void ApplyResultMiddleware(ObjectFieldDefinition mutation)
182182

183183
private void TryApplyInputConvention(
184184
IResolverCompiler resolverCompiler,
185-
ObjectFieldDefinition mutation,
185+
ObjectFieldDefinition mutation,
186186
Options options)
187187
{
188188
if (mutation.Arguments.Count is 0)
@@ -208,7 +208,7 @@ private void TryApplyInputConvention(
208208
mutation.SourceType,
209209
mutation.ResolverType,
210210
argumentNameMap);
211-
211+
212212
TypeMemHelper.Return(argumentNameMap);
213213
}
214214

@@ -250,7 +250,7 @@ private void TryApplyInputConvention(
250250
argumentType,
251251
Path.Root);
252252
}
253-
253+
254254
resolverArguments.Add(
255255
new ResolverArgument(
256256
argument.Name,
@@ -637,7 +637,7 @@ static void CreateErrorDefinitions(
637637
var definition = new ErrorDefinition(
638638
errorType,
639639
schemaType,
640-
obj => obj);
640+
_ => null);
641641
tempErrors.Add(definition);
642642
}
643643
}
@@ -734,7 +734,7 @@ private static Options CreateErrorOptions(
734734
var registeredType = _typeInitializer.InitializeType(type);
735735
_typeInitializer.CompleteTypeName(registeredType);
736736

737-
if (registeredType.Type is ObjectType errorObject &&
737+
if (registeredType.Type is ObjectType errorObject &&
738738
errorObject.RuntimeType != typeof(object))
739739
{
740740
foreach (var possibleInterface in _typeRegistry.Types)
@@ -757,7 +757,7 @@ private static Options CreateErrorOptions(
757757
}
758758
}
759759
}
760-
else if (registeredType.Type is ObjectType errorInterface &&
760+
else if (registeredType.Type is ObjectType errorInterface &&
761761
errorInterface.RuntimeType != typeof(object))
762762
{
763763
foreach (var possibleInterface in _typeRegistry.Types)

src/HotChocolate/Core/test/Types.Mutations.Tests/ErrorMiddlewareTests.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,34 @@ private ValueTask<IRequestExecutor> BuildSchemaAsync(
306306
return builder.BuildRequestExecutorAsync();
307307
}
308308

309+
[Fact]
310+
public async Task Throw_Unexpected_Exception()
311+
{
312+
var executor =
313+
await new ServiceCollection()
314+
.AddGraphQL()
315+
.AddQueryType(d => d.Name("Query").Field("foo").Resolve("bar"))
316+
.AddMutationType<Mutation>()
317+
.AddMutationConventions()
318+
.BuildRequestExecutorAsync();
319+
320+
var result = await executor.ExecuteAsync(
321+
"""
322+
mutation {
323+
createSomething(input: { success: false }) {
324+
book {
325+
title
326+
}
327+
errors {
328+
__typename
329+
}
330+
}
331+
}
332+
""");
333+
334+
result.MatchSnapshot();
335+
}
336+
309337
public class CustomNullRef
310338
{
311339
public CustomNullRef(NullReferenceException exception)
@@ -432,4 +460,19 @@ public class Payload
432460
{
433461
public string Foo() => "Bar";
434462
}
463+
464+
public class Mutation
465+
{
466+
public MutationResult<Book, SomeError> CreateSomething(bool success)
467+
{
468+
throw new Exception("Broken");
469+
}
470+
}
471+
472+
public record Book(string Title);
473+
474+
public class SomeError
475+
{
476+
public string Message => "Some error message";
477+
}
435478
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"errors": [
3+
{
4+
"message": "Unexpected Execution Error",
5+
"locations": [
6+
{
7+
"line": 2,
8+
"column": 3
9+
}
10+
],
11+
"path": [
12+
"createSomething"
13+
]
14+
}
15+
],
16+
"data": null
17+
}

0 commit comments

Comments
 (0)