Skip to content

Commit

Permalink
Fixed Variable Default Value Handling. (ChilliCream#4357)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib authored Oct 29, 2021
1 parent 81ed1e3 commit ad2b4c5
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public void CoerceVariableValues(

if (!hasValue && variableDefinition.DefaultValue is { } defaultValue)
{
value = defaultValue.Kind == SyntaxKind.NullValue ? null : defaultValue;
value = defaultValue.Kind is SyntaxKind.NullValue ? null : defaultValue;
hasValue = true;
}

if (!hasValue || value is null || value is NullValueNode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ await executor.ExecuteAsync(@"
await foreach (IQueryResult queryResult in
subscriptionResult.ReadResultsAsync().WithCancellation(cts.Token))
{
var item = (IReadOnlyQueryResult) queryResult;
var item = (IReadOnlyQueryResult)queryResult;
eventResult = item;
break;
}
Expand Down Expand Up @@ -610,7 +610,7 @@ await executor.ExecuteAsync(@"
await foreach (IQueryResult queryResult in
subscriptionResult.ReadResultsAsync().WithCancellation(cts.Token))
{
var item = (IReadOnlyQueryResult) queryResult;
var item = (IReadOnlyQueryResult)queryResult;
eventResult = item;
break;
}
Expand Down Expand Up @@ -656,7 +656,7 @@ await executor.ExecuteAsync(@"
await foreach (IQueryResult queryResult in
subscriptionResult.ReadResultsAsync().WithCancellation(cts.Token))
{
var item = (IReadOnlyQueryResult) queryResult;
var item = (IReadOnlyQueryResult)queryResult;
eventResult = item;
break;
}
Expand Down Expand Up @@ -700,7 +700,7 @@ await executor.ExecuteAsync(@"
await foreach (IQueryResult queryResult in
subscriptionResult.ReadResultsAsync().WithCancellation(cts.Token))
{
var item = (IReadOnlyQueryResult) queryResult;
var item = (IReadOnlyQueryResult)queryResult;
eventResult = item;
break;
}
Expand Down Expand Up @@ -827,7 +827,7 @@ name @skip(if: $if)
height
}
}",
request: r=> r.SetVariableValue("if", ifValue))
request: r => r.SetVariableValue("if", ifValue))
.MatchSnapshotAsync();
}

Expand All @@ -847,6 +847,36 @@ await ExpectValid(@"
.MatchSnapshotAsync();
}

[Fact]
public async Task SkipAll_Default_False()
{
Snapshot.FullName();

await ExpectValid(@"
query ($if: Boolean! = false) {
human(id: ""1000"") @skip(if: $if) {
name
height
}
}")
.MatchSnapshotAsync();
}

[Fact]
public async Task SkipAll_Default_True()
{
Snapshot.FullName();

await ExpectValid(@"
query ($if: Boolean! = true) {
human(id: ""1000"") @skip(if: $if) {
name
height
}
}")
.MatchSnapshotAsync();
}

[Fact]
public async Task SkipAllSecondLevelFields()
{
Expand All @@ -858,7 +888,7 @@ await ExpectValid(@"
name @skip(if: $if)
}
}",
request: r=> r.SetVariableValue("if", true))
request: r => r.SetVariableValue("if", true))
.MatchSnapshotAsync();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"data": {
"human": {
"name": "Luke Skywalker",
"height": 1.72
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"data": {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,44 @@ public void Coerce_Nullable_String_Variable_With_Default_Where_Value_Is_Not_Prov
helper.CoerceVariableValues(schema, variableDefinitions, variableValues, coercedValues);

// assert
Assert.Empty(coercedValues);
Assert.Collection(coercedValues,
t =>
{
Assert.Equal("abc", t.Key);
Assert.Equal("String", Assert.IsType<StringType>(t.Value.Type).Name);
Assert.Equal("def", t.Value.Value);
Assert.Equal("def", Assert.IsType<StringValueNode>(t.Value.ValueLiteral).Value);
});
}

[Fact]
public void Coerce_Nullable_String_Variable_Where_Value_Is_Not_Provided()
{
// arrange
ISchema schema = SchemaBuilder.New().AddStarWarsTypes().Create();

var variableDefinitions = new List<VariableDefinitionNode>
{
new VariableDefinitionNode(
null,
new VariableNode("abc"),
new NamedTypeNode("String"),
null,
Array.Empty<DirectiveNode>())
};

var variableValues = new Dictionary<string, object>();
var coercedValues = new Dictionary<string, VariableValueOrLiteral>();

var helper = new VariableCoercionHelper(new(), new(new DefaultTypeConverter()));

// act
helper.CoerceVariableValues(schema, variableDefinitions, variableValues, coercedValues);

// assert
Assert.Empty(coercedValues);
}

[Fact]
public void Coerce_Nullable_String_Variable_With_Default_Where_Value_Is_Provided()
{
Expand Down

0 comments on commit ad2b4c5

Please sign in to comment.