Skip to content

Commit 8bb4963

Browse files
committed
init
1 parent 2285351 commit 8bb4963

3 files changed

Lines changed: 15 additions & 17 deletions

File tree

src/main/java/graphql/schema/idl/ArgValueOfAllowedTypeChecker.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import graphql.schema.GraphQLScalarType;
3232
import graphql.schema.idl.errors.DirectiveIllegalArgumentTypeError;
3333

34+
import java.util.Collections;
3435
import java.util.List;
3536
import java.util.Locale;
3637
import java.util.Map;
@@ -41,7 +42,6 @@
4142
import static graphql.collect.ImmutableKit.emptyList;
4243
import static graphql.schema.idl.errors.DirectiveIllegalArgumentTypeError.DUPLICATED_KEYS_MESSAGE;
4344
import static graphql.schema.idl.errors.DirectiveIllegalArgumentTypeError.EXPECTED_ENUM_MESSAGE;
44-
import static graphql.schema.idl.errors.DirectiveIllegalArgumentTypeError.EXPECTED_LIST_MESSAGE;
4545
import static graphql.schema.idl.errors.DirectiveIllegalArgumentTypeError.EXPECTED_NON_NULL_MESSAGE;
4646
import static graphql.schema.idl.errors.DirectiveIllegalArgumentTypeError.EXPECTED_OBJECT_MESSAGE;
4747
import static graphql.schema.idl.errors.DirectiveIllegalArgumentTypeError.MISSING_REQUIRED_FIELD_MESSAGE;
@@ -259,25 +259,24 @@ private void checkArgValueMatchesAllowedNonNullType(List<GraphQLError> errors, V
259259
}
260260

261261
private void checkArgValueMatchesAllowedListType(List<GraphQLError> errors, Value<?> instanceValue, ListType allowedArgType) {
262-
if (instanceValue instanceof NullValue) {
263-
return;
262+
// From the spec, on input coercion:
263+
// If the value passed as an input to a list type is not a list and not the null value,
264+
// then the result of input coercion is a list of size one where the single item value
265+
// is the result of input coercion for the list’s item type on the provided value
266+
// (note this may apply recursively for nested lists).
267+
268+
Value<?> coercedInstanceValue = instanceValue;
269+
if (!(instanceValue instanceof ArrayValue) && !(instanceValue instanceof NullValue)) {
270+
coercedInstanceValue = new ArrayValue(Collections.singletonList(instanceValue));
264271
}
265272

266-
Type<?> unwrappedAllowedType = allowedArgType.getType();
267-
if (!(instanceValue instanceof ArrayValue)) {
268-
checkArgValueMatchesAllowedType(errors, instanceValue, unwrappedAllowedType);
273+
if (coercedInstanceValue instanceof NullValue) {
269274
return;
270275
}
271276

272-
ArrayValue arrayValue = ((ArrayValue) instanceValue);
273-
boolean isUnwrappedList = unwrappedAllowedType instanceof ListType;
274-
275-
// validate each instance value in the list, all instances must match for the list to match
277+
Type<?> unwrappedAllowedType = allowedArgType.getType();
278+
ArrayValue arrayValue = ((ArrayValue) coercedInstanceValue);
276279
arrayValue.getValues().forEach(value -> {
277-
// restrictive check for sub-arrays
278-
if (isUnwrappedList && !(value instanceof ArrayValue)) {
279-
addValidationError(errors, EXPECTED_LIST_MESSAGE, value.getClass().getSimpleName());
280-
}
281280
checkArgValueMatchesAllowedType(errors, value, unwrappedAllowedType);
282281
});
283282
}

src/main/java/graphql/schema/idl/errors/DirectiveIllegalArgumentTypeError.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ public class DirectiveIllegalArgumentTypeError extends BaseError {
1616
public static final String NOT_A_VALID_SCALAR_LITERAL_MESSAGE = "Argument value is not a valid value of scalar '%s'.";
1717
public static final String MISSING_REQUIRED_FIELD_MESSAGE = "Missing required field '%s'.";
1818
public static final String EXPECTED_NON_NULL_MESSAGE = "Argument value is 'null', expected a non-null value.";
19-
public static final String EXPECTED_LIST_MESSAGE = "Argument value is '%s', expected a list value.";
2019
public static final String EXPECTED_OBJECT_MESSAGE = "Argument value is of type '%s', expected an Object value.";
2120

2221
public DirectiveIllegalArgumentTypeError(Node element, String elementName, String directiveName, String argumentName, String detailedMessaged) {

src/test/groovy/graphql/schema/idl/SchemaTypeCheckerTest.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import spock.lang.Unroll
2121
import static graphql.schema.GraphQLScalarType.newScalar
2222
import static graphql.schema.idl.errors.DirectiveIllegalArgumentTypeError.DUPLICATED_KEYS_MESSAGE
2323
import static graphql.schema.idl.errors.DirectiveIllegalArgumentTypeError.EXPECTED_ENUM_MESSAGE
24-
import static graphql.schema.idl.errors.DirectiveIllegalArgumentTypeError.EXPECTED_LIST_MESSAGE
2524
import static graphql.schema.idl.errors.DirectiveIllegalArgumentTypeError.EXPECTED_NON_NULL_MESSAGE
2625
import static graphql.schema.idl.errors.DirectiveIllegalArgumentTypeError.EXPECTED_OBJECT_MESSAGE
2726
import static graphql.schema.idl.errors.DirectiveIllegalArgumentTypeError.MISSING_REQUIRED_FIELD_MESSAGE
@@ -1514,11 +1513,11 @@ class SchemaTypeCheckerTest extends Specification {
15141513
"ACustomDate" | '"AFailingDate"' | format(NOT_A_VALID_SCALAR_LITERAL_MESSAGE, "ACustomDate")
15151514
"[String!]" | '["str", null]' | format(EXPECTED_NON_NULL_MESSAGE)
15161515
"[[String!]!]" | '[["str"], ["str2", null]]' | format(EXPECTED_NON_NULL_MESSAGE)
1516+
"[[String!]!]" | '[["str"], ["str2", "str3"], null]' | format(EXPECTED_NON_NULL_MESSAGE)
15171517
"WEEKDAY" | '"somestr"' | format(EXPECTED_ENUM_MESSAGE, "StringValue")
15181518
"WEEKDAY" | 'SATURDAY' | format(MUST_BE_VALID_ENUM_VALUE_MESSAGE, "SATURDAY", "MONDAY,TUESDAY")
15191519
"UserInput" | '{ fieldNonNull: "str", fieldNonNull: "dupeKey" }' | format(DUPLICATED_KEYS_MESSAGE, "fieldNonNull")
15201520
"UserInput" | '{ fieldNonNull: "str", unknown: "field" }' | format(UNKNOWN_FIELDS_MESSAGE, "unknown", "UserInput")
1521-
"UserInput" | '{ fieldNonNull: "str", fieldArrayOfArray: ["ArrayInsteadOfArrayOfArray"] }' | format(EXPECTED_LIST_MESSAGE, "StringValue")
15221521
"UserInput" | '{ fieldNonNull: "str", fieldNestedInput: "strInsteadOfObject" }' | format(EXPECTED_OBJECT_MESSAGE, "StringValue")
15231522
"UserInput" | '{ field: "missing the `fieldNonNull` entry"}' | format(MISSING_REQUIRED_FIELD_MESSAGE, "fieldNonNull")
15241523
}
@@ -1570,6 +1569,7 @@ class SchemaTypeCheckerTest extends Specification {
15701569
"[String]" | 'null'
15711570
"[String!]!" | '["str"]'
15721571
"[[String!]!]" | '[["str"], ["str2", "str3"]]'
1572+
"[[String!]]" | '[["str"], ["str2", "str3"], null]'
15731573
"WEEKDAY" | 'MONDAY'
15741574
"UserInput" | '{ fieldNonNull: "str" }'
15751575
"UserInput" | '{ fieldNonNull: "str", fieldString: "Hey" }'

0 commit comments

Comments
 (0)