Skip to content

Commit

Permalink
Fixes how argument values will be handled when the input object is in…
Browse files Browse the repository at this point in the history
…ferred. (#6632)
  • Loading branch information
michaelstaib authored Oct 25, 2023
1 parent a4a04a5 commit 4908039
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,32 @@ public async ValueTask InvokeAsync(IMiddlewareContext context)

var arguments = new Dictionary<string, ArgumentValue>(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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<MutationWithOptionalArg>()
.AddMutationConventions()
.ExecuteRequestAsync(
"""
mutation {
doSomething(input: { }) {
string
}
}
""");


result.MatchInlineSnapshot(
"""
{
"data": {
"doSomething": {
"string": "nothing"
}
}
}
""");
}

public class SimpleMutation
{
public string DoSomething(string something)
Expand Down Expand Up @@ -1531,4 +1560,10 @@ public override void OnConfigure(IDescriptorContext context, ObjectFieldDefiniti
}));
}
}

public class MutationWithOptionalArg
{
public string DoSomething(Optional<string?> something)
=> something.Value ?? "nothing";
}
}

0 comments on commit 4908039

Please sign in to comment.