All Variable Usages are Allowed
Formal Specification
- For each operation in document
- Let variableUsages be all usages transitively included in the operation
- For each
variableUsage
in
variableUsages
- Let variableType be the type of variable definition in the operation
- Let argumentType be the type of the argument the variable is passed to.
- Let hasDefault be true if the variable definition defines a default.
- AreTypesCompatible( argumentType , variableType , hasDefault ) must be true
- AreTypesCompatible(
argumentType
,
variableType
,
hasDefault
):
- If hasDefault is true, treat the variableType as non‐null.
- If inner type of argumentType and variableType are different, return false
- If argumentType and variableType have different list dimensions, return false
- If any list level of variableType is not non‐null, and the corresponding level in argument is non‐null, the types are not compatible.
Explanatory Text
Variable usages must be compatible with the arguments they are passed to.
Validation failures occur when variables are used in the context of types that are complete mismatches, or if a nullable type in a variable is passed to a non‐null argument type.
Types must match:
query intCannotGoIntoBoolean($intArg: Int) {
arguments {
booleanArgField(booleanArg: $intArg)
}
}
$intArgtyped asIntcannot be used as a argument tobooleanArg, typed asBoolean.
List cardinality must also be the same. For example, lists cannot be passed into singular values.
query booleanListCannotGoIntoBoolean($booleanListArg: [Boolean]) {
arguments {
booleanArgField(booleanArg: $booleanListArg)
}
}
Nullability must also be respected. In general a nullable variable cannot be passed to a non‐null argument.
query booleanArgQuery($booleanArg: Boolean) {
arguments {
nonNullBooleanArgField(nonNullBooleanArg: $booleanArg)
}
}
A notable exception is when default arguments are provided. They are, in effect, treated as non‐nulls.
query booleanArgQueryWithDefault($booleanArg: Boolean = true) {
arguments {
nonNullBooleanArgField(nonNullBooleanArg: $booleanArg)
}
}
For list types, the same rules around nullability apply to both outer types and inner types. A nullable list cannot be passed to a non‐null list, and a list of nullable values cannot be passed to a list of non‐null values. The following is valid:
query nonNullListToList($nonNullBooleanList: [Boolean]!) {
arguments {
booleanListArgField(booleanListArg: $nonNullBooleanList)
}
}
However, a nullable list cannot be passed to a non‐null list:
query listToNonNullList($booleanList: [Boolean]) {
arguments {
nonNullBooleanListField(nonNullBooleanListArg: $booleanList)
}
}
This would fail validation because a[T]
cannot be passed to a[T]!
.
Similarly a[T]
cannot be passed to a[T!]
.