Skip to content

Commit 3e96bca

Browse files
committed
Moved validation from OneOf-specific rule to VariablesInAllowedPositions
1 parent 16a39a8 commit 3e96bca

File tree

26 files changed

+206
-239
lines changed

26 files changed

+206
-239
lines changed

dictionary.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ BCPROCKS
1818
bfnrt
1919
blazor
2020
blazorwasm
21+
Brontie
2122
Browsable
2223
BSON
2324
buildtransitive

src/HotChocolate/Core/src/Validation/ErrorHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ public static IError OneOfMustHaveExactlyOneField(
649649
.AddLocation(node)
650650
.SetPath(context.CreateErrorPath())
651651
.SetExtension(nameof(type), type.Name)
652-
.SpecifiedBy("sec-OneOf-Input-Objects-Have-Exactly-One-Field", rfc: 825)
652+
.SpecifiedBy("sec-All-Variable-Usages-Are-Allowed", rfc: 825)
653653
.Build();
654654

655655
public static IError OneOfVariablesMustBeNonNull(
@@ -666,7 +666,7 @@ public static IError OneOfVariablesMustBeNonNull(
666666
.AddLocation(node)
667667
.SetPath(context.CreateErrorPath())
668668
.SetFieldCoordinate(fieldCoordinate)
669-
.SpecifiedBy("sec-OneOf–Input-Objects-Have-Exactly-One-Field", rfc: 825)
669+
.SpecifiedBy("sec-All-Variable-Usages-Are-Allowed", rfc: 825)
670670
.Build();
671671

672672
public static IError SkipAndIncludeNotAllowedOnSubscriptionRootField(

src/HotChocolate/Core/src/Validation/Rules/VariableVisitor.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ protected override ISyntaxVisitorAction Enter(
250250
if (context.Variables.TryGetValue(
251251
node.Name.Value,
252252
out var variableDefinition)
253-
&& !IsVariableUsageAllowed(variableDefinition, context.Types.Peek(), defaultValue))
253+
&& !IsVariableUsageAllowed(variableDefinition, context, defaultValue))
254254
{
255255
context.ReportError(context.VariableIsNotCompatible(node, variableDefinition));
256256
}
@@ -281,10 +281,12 @@ protected override ISyntaxVisitorAction Leave(
281281
// http://facebook.github.io/graphql/June2018/#IsVariableUsageAllowed()
282282
private bool IsVariableUsageAllowed(
283283
VariableDefinitionNode variableDefinition,
284-
IType locationType,
284+
DocumentValidatorContext context,
285285
IValueNode? locationDefault)
286286
{
287-
if (locationType.IsNonNullType()
287+
var locationType = context.Types.Peek();
288+
289+
if (IsNonNullPosition(locationType, context)
288290
&& !variableDefinition.Type.IsNonNullType())
289291
{
290292
if (variableDefinition.DefaultValue.IsNull()
@@ -303,6 +305,23 @@ private bool IsVariableUsageAllowed(
303305
locationType);
304306
}
305307

308+
private static bool IsNonNullPosition(IType locationType, DocumentValidatorContext context)
309+
{
310+
if (locationType.IsNonNullType())
311+
{
312+
return true;
313+
}
314+
315+
if (context.Path.Peek() is ObjectFieldNode
316+
&& context.Types[^2].NullableType() is IInputObjectTypeDefinition inputObjectType
317+
&& inputObjectType.Directives.ContainsName(DirectiveNames.OneOf.Name))
318+
{
319+
return true;
320+
}
321+
322+
return false;
323+
}
324+
306325
// http://facebook.github.io/graphql/June2018/#AreTypesCompatible()
307326
private bool AreTypesCompatible(
308327
ITypeNode variableType,

src/HotChocolate/Core/test/Types.Tests/Types/__snapshots__/OneOfIntegrationTests.A_is_null_Error.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
],
1414
"extensions": {
1515
"type": "ExampleInput",
16-
"specifiedBy": "https://spec.graphql.org/draft/#sec-OneOf-Input-Objects-Have-Exactly-One-Field",
16+
"specifiedBy": "https://spec.graphql.org/draft/#sec-All-Variable-Usages-Are-Allowed",
1717
"rfc": "https://github.com/graphql/graphql-spec/pull/825"
1818
}
1919
}

src/HotChocolate/Core/test/Types.Tests/Types/__snapshots__/OneOfIntegrationTests.A_is_null_and_B_is_null_Error.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
],
1414
"extensions": {
1515
"type": "ExampleInput",
16-
"specifiedBy": "https://spec.graphql.org/draft/#sec-OneOf-Input-Objects-Have-Exactly-One-Field",
16+
"specifiedBy": "https://spec.graphql.org/draft/#sec-All-Variable-Usages-Are-Allowed",
1717
"rfc": "https://github.com/graphql/graphql-spec/pull/825"
1818
}
1919
}

src/HotChocolate/Core/test/Types.Tests/Types/__snapshots__/OneOfIntegrationTests.A_is_null_and_B_is_set_Error.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{
1+
{
22
"errors": [
33
{
44
"message": "The OneOf Input Object `ExampleInput` requires that exactly one field must be supplied and that field must not be `null`.",
@@ -13,7 +13,7 @@
1313
],
1414
"extensions": {
1515
"type": "ExampleInput",
16-
"specifiedBy": "https://spec.graphql.org/draft/#sec-OneOf-Input-Objects-Have-Exactly-One-Field",
16+
"specifiedBy": "https://spec.graphql.org/draft/#sec-All-Variable-Usages-Are-Allowed",
1717
"rfc": "https://github.com/graphql/graphql-spec/pull/825"
1818
}
1919
}

src/HotChocolate/Core/test/Types.Tests/Types/__snapshots__/OneOfIntegrationTests.A_is_set_and_B_is_null_Error.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{
1+
{
22
"errors": [
33
{
44
"message": "The OneOf Input Object `ExampleInput` requires that exactly one field must be supplied and that field must not be `null`.",
@@ -13,7 +13,7 @@
1313
],
1414
"extensions": {
1515
"type": "ExampleInput",
16-
"specifiedBy": "https://spec.graphql.org/draft/#sec-OneOf-Input-Objects-Have-Exactly-One-Field",
16+
"specifiedBy": "https://spec.graphql.org/draft/#sec-All-Variable-Usages-Are-Allowed",
1717
"rfc": "https://github.com/graphql/graphql-spec/pull/825"
1818
}
1919
}

src/HotChocolate/Core/test/Types.Tests/Types/__snapshots__/OneOfIntegrationTests.A_is_set_and_B_is_set_Error.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{
1+
{
22
"errors": [
33
{
44
"message": "The OneOf Input Object `ExampleInput` requires that exactly one field must be supplied and that field must not be `null`.",
@@ -13,7 +13,7 @@
1313
],
1414
"extensions": {
1515
"type": "ExampleInput",
16-
"specifiedBy": "https://spec.graphql.org/draft/#sec-OneOf-Input-Objects-Have-Exactly-One-Field",
16+
"specifiedBy": "https://spec.graphql.org/draft/#sec-All-Variable-Usages-Are-Allowed",
1717
"rfc": "https://github.com/graphql/graphql-spec/pull/825"
1818
}
1919
}

src/HotChocolate/Core/test/Types.Tests/Types/__snapshots__/OneOfIntegrationTests.A_is_set_and_B_is_set_to_string_Error.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{
1+
{
22
"errors": [
33
{
44
"message": "The OneOf Input Object `ExampleInput` requires that exactly one field must be supplied and that field must not be `null`.",
@@ -13,7 +13,7 @@
1313
],
1414
"extensions": {
1515
"type": "ExampleInput",
16-
"specifiedBy": "https://spec.graphql.org/draft/#sec-OneOf-Input-Objects-Have-Exactly-One-Field",
16+
"specifiedBy": "https://spec.graphql.org/draft/#sec-All-Variable-Usages-Are-Allowed",
1717
"rfc": "https://github.com/graphql/graphql-spec/pull/825"
1818
}
1919
},

src/HotChocolate/Core/test/Types.Tests/Types/__snapshots__/OneOfIntegrationTests.A_is_unset_variable_and_B_is_set_Error.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
],
1414
"extensions": {
1515
"type": "ExampleInput",
16-
"specifiedBy": "https://spec.graphql.org/draft/#sec-OneOf-Input-Objects-Have-Exactly-One-Field",
16+
"specifiedBy": "https://spec.graphql.org/draft/#sec-All-Variable-Usages-Are-Allowed",
1717
"rfc": "https://github.com/graphql/graphql-spec/pull/825"
1818
}
1919
}

0 commit comments

Comments
 (0)