diff --git a/src/HotChocolate/Core/src/Types.Mutations/MutationConventionMiddleware.cs b/src/HotChocolate/Core/src/Types.Mutations/MutationConventionMiddleware.cs index d09b9489872..871f07e76ad 100644 --- a/src/HotChocolate/Core/src/Types.Mutations/MutationConventionMiddleware.cs +++ b/src/HotChocolate/Core/src/Types.Mutations/MutationConventionMiddleware.cs @@ -35,27 +35,32 @@ public async ValueTask InvokeAsync(IMiddlewareContext context) var arguments = new Dictionary(StringComparer.Ordinal); var preservedArguments = context.ReplaceArguments(arguments); - var inputArgument = preservedArguments[_inputArgumentName]; foreach (var argument in _resolverArguments) { input.TryGetValue(argument.Name, out var value); - inputLiteral.TryGetValue(argument.Name, out var valueLiteral); + var omitted = false; + if (!inputLiteral.TryGetValue(argument.Name, out var valueLiteral)) + { + omitted = true; + valueLiteral = argument.DefaultValue; + value = null; + } valueLiteral ??= NullValueNode.Default; if (!valueLiteral.TryGetValueKind(out var kind)) { kind = ValueKind.Unknown; } - + arguments.Add( argument.Name, new ArgumentValue( argument, kind, - true, - inputArgument.IsDefaultValue, + !omitted, + omitted, value, valueLiteral)); } diff --git a/src/HotChocolate/Core/test/Types.Mutations.Tests/AnnotationBasedMutations.cs b/src/HotChocolate/Core/test/Types.Mutations.Tests/AnnotationBasedMutations.cs index 4a4d5974109..69385c0ba42 100644 --- a/src/HotChocolate/Core/test/Types.Mutations.Tests/AnnotationBasedMutations.cs +++ b/src/HotChocolate/Core/test/Types.Mutations.Tests/AnnotationBasedMutations.cs @@ -1137,8 +1137,37 @@ public async Task Mutation_Aggregate_Error_Not_Mapped() result.MatchSnapshot(); } + + [Fact] + public async Task Mutation_With_Optional_Arg() + { + var result = + await new ServiceCollection() + .AddGraphQL() + .AddQueryType(d => d.Field("abc").Resolve("def")) + .AddMutationType() + .AddMutationConventions() + .ExecuteRequestAsync( + """ + mutation { + doSomething(input: { }) { + string + } + } + """); - + result.MatchInlineSnapshot( + """ + { + "data": { + "doSomething": { + "string": "nothing" + } + } + } + """); + } + public class SimpleMutation { public string DoSomething(string something) @@ -1531,4 +1560,10 @@ public override void OnConfigure(IDescriptorContext context, ObjectFieldDefiniti })); } } + + public class MutationWithOptionalArg + { + public string DoSomething(Optional something) + => something.Value ?? "nothing"; + } } \ No newline at end of file