Skip to content

Commit c423676

Browse files
committed
Simplify coercion algorithm in get_variable_values/get_argument_values
Replicates graphql/graphql-js@18f1f79
1 parent 67fce02 commit c423676

File tree

1 file changed

+23
-42
lines changed

1 file changed

+23
-42
lines changed

src/graphql/execution/values.py

Lines changed: 23 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -83,24 +83,19 @@ def get_variable_values(
8383
)
8484
)
8585
elif has_value:
86-
if value is None:
87-
# If the explicit value `None` was provided, an entry in the
88-
# coerced values must exist as the value `None`.
89-
coerced_values[var_name] = None
86+
# Otherwise, a non-null value was provided, coerce it to the expected
87+
# type or report an error if coercion fails.
88+
coerced = coerce_value(value, var_type, var_def_node)
89+
coercion_errors = coerced.errors
90+
if coercion_errors:
91+
for error in coercion_errors:
92+
error.message = (
93+
f"Variable '${var_name}' got invalid"
94+
f" value {inspect(value)}; {error.message}"
95+
)
96+
errors.extend(coercion_errors)
9097
else:
91-
# Otherwise, a non-null value was provided, coerce it to the
92-
# expected type or report an error if coercion fails.
93-
coerced = coerce_value(value, var_type, var_def_node)
94-
coercion_errors = coerced.errors
95-
if coercion_errors:
96-
for error in coercion_errors:
97-
error.message = (
98-
f"Variable '${var_name}' got invalid"
99-
f" value {inspect(value)}; {error.message}"
100-
)
101-
errors.extend(coercion_errors)
102-
else:
103-
coerced_values[var_name] = coerced.value
98+
coerced_values[var_name] = coerced.value
10499
return (
105100
CoercedVariableValues(errors, None)
106101
if errors
@@ -164,31 +159,17 @@ def get_argument_values(
164159
node,
165160
)
166161
elif has_value:
167-
if isinstance(argument_node.value, NullValueNode):
168-
# If the explicit value `None` was provided, an entry in the coerced
169-
# values must exist as the value `None`.
170-
coerced_values[arg_def.out_name or name] = None
171-
elif isinstance(argument_node.value, VariableNode):
172-
variable_name = argument_node.value.name.value
173-
# Note: This Does no further checking that this variable is correct.
174-
# This assumes that this query has been validated and the variable
175-
# usage here is of the correct type.
176-
coerced_values[arg_def.out_name or name] = variable_values[
177-
variable_name
178-
]
179-
else:
180-
value_node = argument_node.value
181-
coerced_value = value_from_ast(value_node, arg_type, variable_values)
182-
if coerced_value is INVALID:
183-
# Note: `values_of_correct_type` validation should catch this before
184-
# execution. This is a runtime check to ensure execution does not
185-
# continue with an invalid argument value.
186-
raise GraphQLError(
187-
f"Argument '{name}'"
188-
f" has invalid value {print_ast(value_node)}.",
189-
argument_node.value,
190-
)
191-
coerced_values[arg_def.out_name or name] = coerced_value
162+
value_node = argument_node.value
163+
coerced_value = value_from_ast(value_node, arg_type, variable_values)
164+
if coerced_value is INVALID:
165+
# Note: `values_of_correct_type` validation should catch this before
166+
# execution. This is a runtime check to ensure execution does not
167+
# continue with an invalid argument value.
168+
raise GraphQLError(
169+
f"Argument '{name}'" f" has invalid value {print_ast(value_node)}.",
170+
argument_node.value,
171+
)
172+
coerced_values[arg_def.out_name or name] = coerced_value
192173
return coerced_values
193174

194175

0 commit comments

Comments
 (0)