Coercing Variable Values
If the operation has defined any variables, then the values for those variables need to be coerced using the input coercion rules of variable’s declared type. If a query error is encountered during input coercion of variable values, then the operation fails without execution.
(
schema
,
operation
,
variableValues
)
- Let coercedValues be an empty unordered Map.
- Let variableDefinitions be the variables defined by operation .
- For each
variableDefinition
in
variableDefinitions
:
- Let variableName be the name of variableDefinition .
- Let variableType be the expected type of variableDefinition .
- Let defaultValue be the default value for variableDefinition .
- Let value be the value provided in variableValues for the name variableName .
- If
value
does not exist (was not provided in
variableValues
):
- If
defaultValue
exists (including
null
):
- Add an entry to coercedValues named variableName with the value defaultValue .
- Otherwise if variableType is a Non‐Nullable type, throw a query error.
- Otherwise, continue to the next variable definition.
- If
defaultValue
exists (including
null
):
- Otherwise, if value cannot be coerced according to the input coercion rules of variableType , throw a query error.
- Let coercedValue be the result of coercing value according to the input coercion rules of variableType .
- Add an entry to coercedValues named variableName with the value coercedValue .
- Return coercedValues .
This algorithm is very similar to
()
.