Variable Default Values Are Correctly Typed

Formal Specification

  • For every operation in a document
  • For every variable on each operation
    • Let variableType be the type of variable
    • If variableType is non‐null it cannot have a default value
    • If variable has a default value it must be of the same type or able to be coerced to variableType

Explanatory Text

Variables defined by operations are allowed to define default values if the type of that variable is not non‐null.

For example the following query will pass validation.

query houseTrainedQuery($atOtherHomes: Boolean = true) {
  dog {
    isHousetrained(atOtherHomes: $atOtherHomes)
  }
}

However if the variable is defined as non‐null, default values are unreachable. Therefore queries such as the following fail validation

query houseTrainedQuery($atOtherHomes: Boolean! = true) {
  dog {
    isHousetrained(atOtherHomes: $atOtherHomes)
  }
}

Default values must be compatible with the types of variables. Types must match or they must be coercible to the type.

Non‐matching types fail, such as in the following example:

query houseTrainedQuery($atOtherHomes: Boolean = "true") {
  dog {
    isHousetrained(atOtherHomes: $atOtherHomes)
  }
}

However if a type is coercible the query will pass validation.

For example:

query intToFloatQuery($floatVar: Float = 1) {
  arguments {
    floatArgField(floatArg: $floatVar)
  }
}

results matching ""

    No results matching ""