From 8631e30660479fa9421bc01134af901411c3b9a8 Mon Sep 17 00:00:00 2001 From: Michael Staib Date: Wed, 2 Oct 2024 18:08:10 +0200 Subject: [PATCH] =?UTF-8?q?Fixed=20issue=20with=20errors=20in=20mutation?= =?UTF-8?q?=20convention=20when=20explicitly=20bindun=E2=80=A6=20(#7548)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Core/src/Types.Errors/ErrorObjectType.cs | 1 + .../src/Types.Errors/ExceptionObjectType.cs | 1 + .../AnnotationBasedMutations.cs | 25 +++++++++++++++ ...ErrorEvenIfExplicitFieldBindingIsUsed.snap | 31 +++++++++++++++++++ 4 files changed, 58 insertions(+) create mode 100644 src/HotChocolate/Core/test/Types.Mutations.Tests/__snapshots__/AnnotationBasedMutations.InferErrorEvenIfExplicitFieldBindingIsUsed.snap diff --git a/src/HotChocolate/Core/src/Types.Errors/ErrorObjectType.cs b/src/HotChocolate/Core/src/Types.Errors/ErrorObjectType.cs index 179e25ef42b..d87ca32898f 100644 --- a/src/HotChocolate/Core/src/Types.Errors/ErrorObjectType.cs +++ b/src/HotChocolate/Core/src/Types.Errors/ErrorObjectType.cs @@ -6,6 +6,7 @@ protected override void Configure(IObjectTypeDescriptor descriptor) { descriptor.Extend().OnBeforeCreate(RewriteMessageFieldToNonNullableStringType); descriptor.Extend().Definition.ContextData.MarkAsError(); + descriptor.BindFieldsImplicitly(); } private void RewriteMessageFieldToNonNullableStringType( diff --git a/src/HotChocolate/Core/src/Types.Errors/ExceptionObjectType.cs b/src/HotChocolate/Core/src/Types.Errors/ExceptionObjectType.cs index 4985c9c5c3b..682e43d4d32 100644 --- a/src/HotChocolate/Core/src/Types.Errors/ExceptionObjectType.cs +++ b/src/HotChocolate/Core/src/Types.Errors/ExceptionObjectType.cs @@ -15,6 +15,7 @@ protected override void Configure(IObjectTypeDescriptor descriptor) descriptor.Ignore(x => x.GetBaseException()); descriptor.Field(x => x.Message).Type>(); descriptor.Extend().Definition.ContextData.MarkAsError(); + descriptor.BindFieldsImplicitly(); } private static string GetNameFromException() diff --git a/src/HotChocolate/Core/test/Types.Mutations.Tests/AnnotationBasedMutations.cs b/src/HotChocolate/Core/test/Types.Mutations.Tests/AnnotationBasedMutations.cs index 85197cc663b..42c0b5743d4 100644 --- a/src/HotChocolate/Core/test/Types.Mutations.Tests/AnnotationBasedMutations.cs +++ b/src/HotChocolate/Core/test/Types.Mutations.Tests/AnnotationBasedMutations.cs @@ -1275,6 +1275,31 @@ public async Task Mutation_With_ErrorAnnotatedAndCustomInterface_LateAndEarlyReg result.Print().MatchSnapshot(); } + [Fact] + public async Task InferErrorEvenIfExplicitFieldBindingIsUsed() + { + var schema = + await new ServiceCollection() + .AddGraphQL() + .AddQueryType(d => d.Field("abc").Resolve("def")) + .AddMutationType(c => c.Field(t => t.DoSomething(default))) + .AddMutationConventions() + .BuildSchemaAsync(); + + schema.Print().MatchSnapshot(); + } + + public class ExplicitMutation + { + public FieldResult DoSomething(int status) + => new ExplicitCustomError { Message = "Error" }; + } + + public sealed class ExplicitCustomError + { + public required string Message { get; set; } + } + public class SimpleMutation { public string DoSomething(string something) diff --git a/src/HotChocolate/Core/test/Types.Mutations.Tests/__snapshots__/AnnotationBasedMutations.InferErrorEvenIfExplicitFieldBindingIsUsed.snap b/src/HotChocolate/Core/test/Types.Mutations.Tests/__snapshots__/AnnotationBasedMutations.InferErrorEvenIfExplicitFieldBindingIsUsed.snap new file mode 100644 index 00000000000..bb6f894e883 --- /dev/null +++ b/src/HotChocolate/Core/test/Types.Mutations.Tests/__snapshots__/AnnotationBasedMutations.InferErrorEvenIfExplicitFieldBindingIsUsed.snap @@ -0,0 +1,31 @@ +schema { + query: Query + mutation: ExplicitMutation +} + +interface Error { + message: String! +} + +type DoSomethingPayload { + int: Int + errors: [DoSomethingError!] +} + +type ExplicitCustomError implements Error { + message: String! +} + +type ExplicitMutation { + doSomething(input: DoSomethingInput!): DoSomethingPayload! +} + +type Query { + abc: String +} + +union DoSomethingError = ExplicitCustomError + +input DoSomethingInput { + status: Int! +}