Skip to content

Commit

Permalink
Fixed issue with errors in mutation convention when explicitly bindun… (
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib committed Oct 2, 2024
1 parent 9cc769e commit 8631e30
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/HotChocolate/Core/src/Types.Errors/ErrorObjectType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ protected override void Configure(IObjectTypeDescriptor<T> descriptor)
{
descriptor.Extend().OnBeforeCreate(RewriteMessageFieldToNonNullableStringType);
descriptor.Extend().Definition.ContextData.MarkAsError();
descriptor.BindFieldsImplicitly();
}

private void RewriteMessageFieldToNonNullableStringType(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ protected override void Configure(IObjectTypeDescriptor<T> descriptor)
descriptor.Ignore(x => x.GetBaseException());
descriptor.Field(x => x.Message).Type<NonNullType<StringType>>();
descriptor.Extend().Definition.ContextData.MarkAsError();
descriptor.BindFieldsImplicitly();
}

private static string GetNameFromException()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ExplicitMutation>(c => c.Field(t => t.DoSomething(default)))
.AddMutationConventions()
.BuildSchemaAsync();

schema.Print().MatchSnapshot();
}

public class ExplicitMutation
{
public FieldResult<int, ExplicitCustomError> 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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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!
}

0 comments on commit 8631e30

Please sign in to comment.